How to use tmpFile method in storybook-test-runner

Best JavaScript code snippet using storybook-test-runner

index.js

Source:index.js Github

copy

Full Screen

1'use strict'2module.exports = writeFile3module.exports.sync = writeFileSync4module.exports._getTmpname = getTmpname // for testing5module.exports._cleanupOnExit = cleanupOnExit6const fs = require('fs')7const MurmurHash3 = require('imurmurhash')8const onExit = require('signal-exit')9const path = require('path')10const isTypedArray = require('is-typedarray')11const typedArrayToBuffer = require('typedarray-to-buffer')12const { promisify } = require('util')13const activeFiles = {}14// if we run inside of a worker_thread, `process.pid` is not unique15/* istanbul ignore next */16const threadId = (function getId () {17 try {18 const workerThreads = require('worker_threads')19 /// if we are in main thread, this is set to `0`20 return workerThreads.threadId21 } catch (e) {22 // worker_threads are not available, fallback to 023 return 024 }25})()26let invocations = 027function getTmpname (filename) {28 return filename + '.' +29 MurmurHash3(__filename)30 .hash(String(process.pid))31 .hash(String(threadId))32 .hash(String(++invocations))33 .result()34}35function cleanupOnExit (tmpfile) {36 return () => {37 try {38 fs.unlinkSync(typeof tmpfile === 'function' ? tmpfile() : tmpfile)39 } catch (_) {}40 }41}42function serializeActiveFile (absoluteName) {43 return new Promise(resolve => {44 // make a queue if it doesn't already exist45 if (!activeFiles[absoluteName]) activeFiles[absoluteName] = []46 activeFiles[absoluteName].push(resolve) // add this job to the queue47 if (activeFiles[absoluteName].length === 1) resolve() // kick off the first one48 })49}50// https://github.com/isaacs/node-graceful-fs/blob/master/polyfills.js#L315-L34251function isChownErrOk (err) {52 if (err.code === 'ENOSYS') {53 return true54 }55 const nonroot = !process.getuid || process.getuid() !== 056 if (nonroot) {57 if (err.code === 'EINVAL' || err.code === 'EPERM') {58 return true59 }60 }61 return false62}63async function writeFileAsync (filename, data, options = {}) {64 if (typeof options === 'string') {65 options = { encoding: options }66 }67 let fd68 let tmpfile69 /* istanbul ignore next -- The closure only gets called when onExit triggers */70 const removeOnExitHandler = onExit(cleanupOnExit(() => tmpfile))71 const absoluteName = path.resolve(filename)72 try {73 await serializeActiveFile(absoluteName)74 const truename = await promisify(fs.realpath)(filename).catch(() => filename)75 tmpfile = getTmpname(truename)76 if (!options.mode || !options.chown) {77 // Either mode or chown is not explicitly set78 // Default behavior is to copy it from original file79 const stats = await promisify(fs.stat)(truename).catch(() => {})80 if (stats) {81 if (options.mode == null) {82 options.mode = stats.mode83 }84 if (options.chown == null && process.getuid) {85 options.chown = { uid: stats.uid, gid: stats.gid }86 }87 }88 }89 fd = await promisify(fs.open)(tmpfile, 'w', options.mode)90 if (options.tmpfileCreated) {91 await options.tmpfileCreated(tmpfile)92 }93 if (isTypedArray(data)) {94 data = typedArrayToBuffer(data)95 }96 if (Buffer.isBuffer(data)) {97 await promisify(fs.write)(fd, data, 0, data.length, 0)98 } else if (data != null) {99 await promisify(fs.write)(fd, String(data), 0, String(options.encoding || 'utf8'))100 }101 if (options.fsync !== false) {102 await promisify(fs.fsync)(fd)103 }104 await promisify(fs.close)(fd)105 fd = null106 if (options.chown) {107 await promisify(fs.chown)(tmpfile, options.chown.uid, options.chown.gid).catch(err => {108 if (!isChownErrOk(err)) {109 throw err110 }111 })112 }113 if (options.mode) {114 await promisify(fs.chmod)(tmpfile, options.mode).catch(err => {115 if (!isChownErrOk(err)) {116 throw err117 }118 })119 }120 await promisify(fs.rename)(tmpfile, truename)121 } finally {122 if (fd) {123 await promisify(fs.close)(fd).catch(124 /* istanbul ignore next */125 () => {}126 )127 }128 removeOnExitHandler()129 await promisify(fs.unlink)(tmpfile).catch(() => {})130 activeFiles[absoluteName].shift() // remove the element added by serializeSameFile131 if (activeFiles[absoluteName].length > 0) {132 activeFiles[absoluteName][0]() // start next job if one is pending133 } else delete activeFiles[absoluteName]134 }135}136function writeFile (filename, data, options, callback) {137 if (options instanceof Function) {138 callback = options139 options = {}140 }141 const promise = writeFileAsync(filename, data, options)142 if (callback) {143 promise.then(callback, callback)144 }145 return promise146}147function writeFileSync (filename, data, options) {148 if (typeof options === 'string') options = { encoding: options }149 else if (!options) options = {}150 try {151 filename = fs.realpathSync(filename)152 } catch (ex) {153 // it's ok, it'll happen on a not yet existing file154 }155 const tmpfile = getTmpname(filename)156 if (!options.mode || !options.chown) {157 // Either mode or chown is not explicitly set158 // Default behavior is to copy it from original file159 try {160 const stats = fs.statSync(filename)161 options = Object.assign({}, options)162 if (!options.mode) {163 options.mode = stats.mode164 }165 if (!options.chown && process.getuid) {166 options.chown = { uid: stats.uid, gid: stats.gid }167 }168 } catch (ex) {169 // ignore stat errors170 }171 }172 let fd173 const cleanup = cleanupOnExit(tmpfile)174 const removeOnExitHandler = onExit(cleanup)175 let threw = true176 try {177 fd = fs.openSync(tmpfile, 'w', options.mode || 0o666)178 if (options.tmpfileCreated) {179 options.tmpfileCreated(tmpfile)180 }181 if (isTypedArray(data)) {182 data = typedArrayToBuffer(data)183 }184 if (Buffer.isBuffer(data)) {185 fs.writeSync(fd, data, 0, data.length, 0)186 } else if (data != null) {187 fs.writeSync(fd, String(data), 0, String(options.encoding || 'utf8'))188 }189 if (options.fsync !== false) {190 fs.fsyncSync(fd)191 }192 fs.closeSync(fd)193 fd = null194 if (options.chown) {195 try {196 fs.chownSync(tmpfile, options.chown.uid, options.chown.gid)197 } catch (err) {198 if (!isChownErrOk(err)) {199 throw err200 }201 }202 }203 if (options.mode) {204 try {205 fs.chmodSync(tmpfile, options.mode)206 } catch (err) {207 if (!isChownErrOk(err)) {208 throw err209 }210 }211 }212 fs.renameSync(tmpfile, filename)213 threw = false214 } finally {215 if (fd) {216 try {217 fs.closeSync(fd)218 } catch (ex) {219 // ignore close errors at this stage, error may have closed fd already.220 }221 }222 removeOnExitHandler()223 if (threw) {224 cleanup()225 }226 }...

Full Screen

Full Screen

parse_spec.js

Source:parse_spec.js Github

copy

Full Screen

1var should = require('should'),2 tmp = require('tmp'),3 fs = require('fs-extra'),4 packageJSON = require('../../../../../server/lib/fs/package-json');5describe('lib/fs/package-json: parse', function () {6 it('should parse valid package.json', function (done) {7 var pkgJson, tmpFile;8 tmpFile = tmp.fileSync();9 pkgJson = JSON.stringify({10 name: 'test',11 version: '0.0.0'12 });13 fs.writeSync(tmpFile.fd, pkgJson);14 packageJSON.parse(tmpFile.name)15 .then(function (pkg) {16 pkg.should.eql({17 name: 'test',18 version: '0.0.0'19 });20 done();21 })22 .catch(done)23 .finally(tmpFile.removeCallback);24 });25 it('should fail when name is missing', function (done) {26 var pkgJson, tmpFile;27 tmpFile = tmp.fileSync();28 pkgJson = JSON.stringify({29 version: '0.0.0'30 });31 fs.writeSync(tmpFile.fd, pkgJson);32 packageJSON.parse(tmpFile.name)33 .then(function () {34 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));35 })36 .catch(function (err) {37 err.message.should.equal('"name" or "version" is missing from theme package.json file.');38 err.context.should.equal(tmpFile.name);39 err.help.should.equal('This will be required in future. Please see http://docs.ghost.org/themes/');40 done();41 })42 .catch(done)43 .finally(tmpFile.removeCallback);44 });45 it('should fail when version is missing', function (done) {46 var pkgJson, tmpFile;47 tmpFile = tmp.fileSync();48 pkgJson = JSON.stringify({49 name: 'test'50 });51 fs.writeSync(tmpFile.fd, pkgJson);52 packageJSON.parse(tmpFile.name)53 .then(function () {54 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));55 })56 .catch(function (err) {57 err.message.should.equal('"name" or "version" is missing from theme package.json file.');58 err.context.should.equal(tmpFile.name);59 err.help.should.equal('This will be required in future. Please see http://docs.ghost.org/themes/');60 done();61 })62 .catch(done)63 .finally(tmpFile.removeCallback);64 });65 it('should fail when JSON is invalid', function (done) {66 var pkgJson, tmpFile;67 tmpFile = tmp.fileSync();68 pkgJson = '{name:"test"}';69 fs.writeSync(tmpFile.fd, pkgJson);70 packageJSON.parse(tmpFile.name)71 .then(function () {72 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));73 })74 .catch(function (err) {75 err.message.should.equal('Theme package.json file is malformed');76 err.context.should.equal(tmpFile.name);77 err.help.should.equal('This will be required in future. Please see http://docs.ghost.org/themes/');78 done();79 })80 .catch(done)81 .finally(tmpFile.removeCallback);82 });83 it('should fail when file is missing', function (done) {84 var tmpFile = tmp.fileSync();85 tmpFile.removeCallback();86 packageJSON.parse(tmpFile.name)87 .then(function () {88 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));89 })90 .catch(function (err) {91 err.message.should.equal('Could not read package.json file');92 err.context.should.equal(tmpFile.name);93 done();94 })95 .catch(done);96 });...

Full Screen

Full Screen

parse.test.js

Source:parse.test.js Github

copy

Full Screen

1require('./utils');2const tmp = require('tmp');3const fs = require('fs-extra');4const parse = require('../lib/parse');5describe('package-json parse', function () {6 it('should parse valid package.json', function (done) {7 let pkgJson;8 let tmpFile;9 tmpFile = tmp.fileSync();10 pkgJson = JSON.stringify({11 name: 'test',12 version: '0.0.0'13 });14 fs.writeSync(tmpFile.fd, pkgJson);15 parse(tmpFile.name)16 .then(function (pkg) {17 pkg.should.eql({18 name: 'test',19 version: '0.0.0'20 });21 done();22 })23 .catch(done)24 .finally(tmpFile.removeCallback);25 });26 it('should fail when name is missing', function (done) {27 let pkgJson;28 let tmpFile;29 tmpFile = tmp.fileSync();30 pkgJson = JSON.stringify({31 version: '0.0.0'32 });33 fs.writeSync(tmpFile.fd, pkgJson);34 parse(tmpFile.name)35 .then(function () {36 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));37 })38 .catch(function (err) {39 err.message.should.equal('"name" or "version" is missing from theme package.json file.');40 err.context.should.equal(tmpFile.name);41 err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/');42 done();43 })44 .catch(done)45 .finally(tmpFile.removeCallback);46 });47 it('should fail when version is missing', function (done) {48 let pkgJson;49 let tmpFile;50 tmpFile = tmp.fileSync();51 pkgJson = JSON.stringify({52 name: 'test'53 });54 fs.writeSync(tmpFile.fd, pkgJson);55 parse(tmpFile.name)56 .then(function () {57 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));58 })59 .catch(function (err) {60 err.message.should.equal('"name" or "version" is missing from theme package.json file.');61 err.context.should.equal(tmpFile.name);62 err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/');63 done();64 })65 .catch(done)66 .finally(tmpFile.removeCallback);67 });68 it('should fail when JSON is invalid', function (done) {69 let pkgJson;70 let tmpFile;71 tmpFile = tmp.fileSync();72 pkgJson = '{name:"test"}';73 fs.writeSync(tmpFile.fd, pkgJson);74 parse(tmpFile.name)75 .then(function () {76 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));77 })78 .catch(function (err) {79 err.message.should.equal('Theme package.json file is malformed');80 err.context.should.equal(tmpFile.name);81 err.help.should.equal('This will be required in future. Please see https://ghost.org/docs/themes/');82 done();83 })84 .catch(done)85 .finally(tmpFile.removeCallback);86 });87 it('should fail when file is missing', function (done) {88 const tmpFile = tmp.fileSync();89 tmpFile.removeCallback();90 parse(tmpFile.name)91 .then(function () {92 done(new Error('packageJSON.parse succeeded, but should\'ve failed'));93 })94 .catch(function (err) {95 err.message.should.equal('Could not read package.json file');96 err.context.should.equal(tmpFile.name);97 done();98 })99 .catch(done);100 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { tmpFile } from 'storybook-test-runner';2const tmpFile = require('storybook-test-runner').tmpFile;3const { tmpFile } = require('storybook-test-runner');4const tmpFile = require('storybook-test-runner').tmpFile;5const { tmpFile } = require('storybook-test-runner');6const tmpFile = require('storybook-test-runner').tmpFile;7const { tmpFile } = require('storybook-test-runner');8const tmpFile = require('storybook-test-runner').tmpFile;9const { tmpFile } = require('storybook-test-runner');10const tmpFile = require('storybook-test-runner').tmpFile;11const { tmpFile } = require('storybook-test-runner');12const tmpFile = require('storybook-test-runner').tmpFile;13const { tmpFile } = require('storybook-test-runner');14const tmpFile = require('storybook-test-runner').tmpFile;15const { tmpFile } = require('storybook-test-runner');16const tmpFile = require('storybook-test-runner').tmpFile;17const { tmpFile } = require('storybook-test-runner');18const tmpFile = require('storybook-test-runner').tmpFile;19const { tmpFile } = require('storybook-test-runner');20const tmpFile = require('storybook-test-runner').tmpFile;

Full Screen

Using AI Code Generation

copy

Full Screen

1import { tmpFile } from 'storybook-test-runner';2import { storiesOf } from '@storybook/react';3import { withKnobs, text } from '@storybook/addon-knobs';4import { withTests } from '@storybook/addon-jest';5import results from '../.jest-test-results.json';6import { Button } from './button';7storiesOf('Button', module)8 .addDecorator(withKnobs)9 .addDecorator(10 withTests({11 })12 .add('with text', () => {13 const label = text('Label', 'Hello Button');14 return <Button>{label}</Button>;15 })16 .add('with some emoji', () => {17 const label = text('Label', '😀 😎 👍 💯');18 return <Button>{label}</Button>;19 });20MIT © [Daniel Gomes](

Full Screen

Using AI Code Generation

copy

Full Screen

1import { tmpFile } from 'storybook-test-runner'2import { tmpFile } from 'storybook-test-runner'3import { tmpFile } from 'storybook-test-runner'4export { tmpFile }5import { tmpFile } from './test'6import { tmpFile } from './test'7import { mount } from 'storybook-test-runner'8import { MyComponent } from 'src/components'9describe('MyComponent', () => {10 it('should call the click handler when the button is clicked', () => {11 const clickHandler = jest.fn()12 const wrapper = mount(MyComponent, { clickHandler })13 wrapper.find('button').simulate('click')14 expect(clickHandler).toBeCalled()15 })16})17import { mount } from 'storybook-test-runner'18import { MyComponent } from 'src/components'19describe('MyComponent', () => {20 it('should call the click handler when the button is clicked', () => {21 const clickHandler = jest.fn()22 const wrapper = mount(MyComponent, {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { tmpFile } from 'storybook-test-runner';2describe('tmpFile', () => {3 it('should create a temporary file', async () => {4 const { path, cleanup } = await tmpFile();5 cleanup();6 });7});8import { tmpDir } from 'storybook-test-runner';9describe('tmpDir', () => {10 it('should create a temporary directory', async () => {11 const { path, cleanup } = await tmpDir();12 cleanup();13 });14});15import { tmpDir } from 'storybook-test-runner';16describe('tmpDir', () => {17 it('should create a temporary directory', async () => {18 const { path, cleanup } = await tmpDir();19 cleanup();20 });21});22import { tmpDir } from 'storybook-test-runner';23describe('tmpDir', () => {24 it('should create a temporary directory', async () => {25 const { path, cleanup } = await tmpDir();26 cleanup();27 });28});29import { tmpDir } from 'storybook-test-runner';30describe('tmpDir', () => {31 it('should create a temporary directory', async () => {32 const { path, cleanup } = await tmpDir();33 cleanup();34 });35});36import { tmpDir } from 'storybook-test-runner';37describe('tmpDir', () => {38 it('should create a temporary directory', async () => {39 const { path, cleanup } = await tmpDir();40 cleanup();41 });42});43import { tmpDir } from 'storybook-test-runner';44describe('tmpDir', () => {45 it('should create a temporary directory', async () => {46 const { path, cleanup } = await tmpDir

Full Screen

Using AI Code Generation

copy

Full Screen

1import { tmpFile } from 'storybook-test-runner';2import { storiesOf } from '@storybook/react';3storiesOf('tmpFile', module)4 .add('tmpFile', () => {5 return tmpFile('test.js', 'console.log("Hello World")');6 });7import { tmpFile } from 'storybook-test-runner';8import { storiesOf } from '@storybook/react';9storiesOf('tmpFile', module)10 .add('tmpFile', () => {11 return tmpFile('test.js', 'console.log("Hello World")');12 });13import { tmpFile } from 'storybook-test-runner';14import { storiesOf } from '@storybook/react';15storiesOf('tmpFile', module)16 .add('tmpFile', () => {17 return tmpFile('test.js', 'console.log("Hello World")');18 });19import { tmpFile } from 'storybook-test-runner';20import { storiesOf } from '@storybook/react';21storiesOf('tmpFile', module)22 .add('tmpFile', () => {23 return tmpFile('test.js', 'console.log("Hello World")');24 });25import { tmpFile } from 'storybook-test-runner';26import { storiesOf } from '@storybook/react';27storiesOf('tmpFile', module)28 .add('tmpFile', () => {29 return tmpFile('test.js', 'console.log("Hello World")');30 });31import { tmpFile } from 'storybook-test-runner';32import { storiesOf } from '@storybook/react';33storiesOf('tmpFile', module)34 .add('tmpFile', () => {35 return tmpFile('test.js', 'console.log("Hello World")');36 });37import { tmpFile } from 'storybook-test-runner';38import { storiesOf } from '@storybook/react';39storiesOf('tmpFile', module)40 .add('tmpFile', () => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const testRunner = require('storybook-test-runner');2const { tmpFile } = testRunner;3const storybook = tmpFile('./storybook');4const testRunner = require('storybook-test-runner');5const { start } = testRunner;6const storybook = start('./storybook');7const testRunner = require('storybook-test-runner');8const { start } = testRunner;9const storybook = start('./storybook', {10});11const testRunner = require('storybook-test-runner');12const { start } = testRunner;13const storybook = start('./storybook', {14}, {15 module: {16 {17 }18 }19});20const testRunner = require('storybook-test-runner');21const { start } = testRunner;22const storybook = start('./storybook', {23}, {24 module: {25 {26 }27 }28}, {29 module: {30 {31 }32 }33});34const testRunner = require('storybook-test-runner');35const { start } = testRunner;36const storybook = start('./storybook', {37}, {38 module: {39 {40 }41 }42}, {

Full Screen

Using AI Code Generation

copy

Full Screen

1import { tmpFile } from 'storybook-test-runner';2import { storiesOf } from '@storybook/react';3storiesOf('MyComponent', module)4 .add('with some text', () => {5 return tmpFile('my-component.js', `6 import MyComponent from './my-component';7 export default MyComponent;8 `);9 });10import React from 'react';11export default () => {12 return <div>My Component</div>;13};14import { storiesOf } from '@storybook/react';15storiesOf('MyComponent', module)16 .add('with some text', () => {17 return <div>My Component</div>;18 });19import React from 'react';20import { shallow } from 'enzyme';21import MyComponent from './my-component';22describe('MyComponent', () => {23 it('renders correctly', () => {24 const wrapper = shallow(<MyComponent />);25 expect(wrapper).toMatchSnapshot();26 });27});28import { storiesOf } from '@storybook/react';29storiesOf('MyComponent', module)30 .add('with some text', () => {31 return <div>My Component</div>;32 });33import { tmpFile } from 'storybook-test-runner';34tmpFile('my-component.js', `35 import React from 'react';36 export default () => {37 return <div>My Component</div>;38 };39`);40import { tmpFileWithStory } from 'storybook-test-runner';41tmpFileWithStory('my-component.js', `42 import React from 'react';43 export default () => {44 return <div>My Component</div>;45 };46`);47import { tmpDir } from 'storybook-test-runner';48tmpDir('my-component', `

Full Screen

Using AI Code Generation

copy

Full Screen

1const {tmpFile} = require('storybook-test-runner');2const {expect} = require('chai');3const storybook = require('./storybook');4describe('Storybook', function() {5 this.timeout(10000);6 it('should render correctly', async () => {7 const file = await tmpFile(storybook, 'Button', 'Default');8 expect(file).to.exist;9 });10});11const path = require('path');12const {Storybook} = require('storybook-test-runner');13const storybook = new Storybook({14 storybookConfigDir: path.resolve(__dirname, '../.storybook'),15 buildDir: path.resolve(__dirname, '../build'),16 staticDir: path.resolve(__dirname, '../public'),17});18module.exports = storybook;

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-test-runner automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful