Best JavaScript code snippet using jest
babel-plugin-extension-resolver.test.js
Source:babel-plugin-extension-resolver.test.js
...27 await transformAsync('import path from "path";', options)28 ).toHaveProperty('code', 'import path from "path";');29 });30 it('finds .js files', async () => {31 fs.__setMockFiles([32 '/path/to/src/other.js',33 '/path/to/src/other.ts',34 '/path/to/src/other.tsx',35 ]);36 expect(37 await transformAsync('import other from "./other";', options)38 ).toHaveProperty('code', 'import other from "./other.js";');39 });40 it('finds .ts files', async () => {41 fs.__setMockFiles(['/path/to/src/other.ts', '/path/to/src/other.tsx']);42 expect(43 await transformAsync('import other from "./other";', options)44 ).toHaveProperty('code', 'import other from "./other.js";');45 });46 it('finds .tsx files', async () => {47 fs.__setMockFiles(['/path/to/src/other.tsx']);48 expect(49 await transformAsync('import other from "./other";', options)50 ).toHaveProperty('code', 'import other from "./other.js";');51 });52 it('finds files in parent directory', async () => {53 fs.__setMockFiles(['/path/to/other.js', '/path/to/src/other.js']);54 expect(55 await transformAsync('import other from "../other";', options)56 ).toHaveProperty('code', 'import other from "../other.js";');57 });58 it('finds files in child directory', async () => {59 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/child/other.js']);60 expect(61 await transformAsync('import other from "./child/other";', options)62 ).toHaveProperty('code', 'import other from "./child/other.js";');63 });64 it('uses index file', async () => {65 fs.__setMockFiles(['/path/to/src/other/index.js']);66 expect(67 await transformAsync('import other from "./other";', options)68 ).toHaveProperty('code', 'import other from "./other/index.js";');69 });70 it('uses modulesToResolve', async () => {71 fs.__setMockFiles(['/path/to/src/other/index.js']);72 expect(73 await transformAsync(74 'import index from "instantsearch.js/es/widgets/index/index";\n' +75 'import root from "instantsearch.js";\n' +76 'import other from "different-module/other";',77 options78 )79 ).toHaveProperty(80 'code',81 'import index from "instantsearch.js/es/widgets/index/index.js";\n' +82 'import root from "instantsearch.js";\n' +83 'import other from "different-module/other";'84 );85 });86 it('works with multiple imports', async () => {87 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);88 expect(89 await transformAsync(90 'import other from "./other";\nimport another from "./another";',91 options92 )93 ).toHaveProperty(94 'code',95 'import other from "./other.js";\nimport another from "./another.js";'96 );97 });98 it('works with export from', async () => {99 fs.__setMockFiles(['/path/to/src/other.js']);100 expect(101 await transformAsync('export * from "./other"', options)102 ).toHaveProperty('code', 'export * from "./other.js";');103 });104 it('ignores require()', async () => {105 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);106 expect(await transformAsync('require("./other");', options)).toHaveProperty(107 'code',108 'require("./other");'109 );110 });111 it('ignores other function calls', async () => {112 fs.__setMockFiles(['/path/to/src/other.js']);113 expect(114 await transformAsync('requireOOPS("./other");', options)115 ).toHaveProperty('code', 'requireOOPS("./other");');116 });117 it('errors if file not found', async () => {118 fs.__setMockFiles([]);119 await expect(120 transformAsync('import other from "./other";', options)121 ).rejects.toThrow(122 '/path/to/src/file.js: import for "./other" could not be resolved'123 );124 });125 it('errors if module file not found', async () => {126 fs.__setMockFiles([]);127 await expect(128 transformAsync(129 'import other from "instantsearch.js/non-existing-folder-this-can-never-exist/qsdf/gh/jklm";',130 options131 )132 ).rejects.toThrow(133 /\/path\/to\/src\/file.js: Cannot find module 'instantsearch.js\/non-existing-folder-this-can-never-exist\/qsdf\/gh\/jklm' from '/134 );135 });...
extension-resolver.test.js
Source:extension-resolver.test.js
...27 await transformAsync('import path from "path";', options)28 ).toHaveProperty('code', 'import path from "path";');29 });30 it('finds .js files', async () => {31 fs.__setMockFiles([32 '/path/to/src/other.js',33 '/path/to/src/other.ts',34 '/path/to/src/other.tsx',35 ]);36 expect(37 await transformAsync('import other from "./other";', options)38 ).toHaveProperty('code', 'import other from "./other.js";');39 });40 it('finds .ts files', async () => {41 fs.__setMockFiles(['/path/to/src/other.ts', '/path/to/src/other.tsx']);42 expect(43 await transformAsync('import other from "./other";', options)44 ).toHaveProperty('code', 'import other from "./other.js";');45 });46 it('finds .tsx files', async () => {47 fs.__setMockFiles(['/path/to/src/other.tsx']);48 expect(49 await transformAsync('import other from "./other";', options)50 ).toHaveProperty('code', 'import other from "./other.js";');51 });52 it('finds files in parent directory', async () => {53 fs.__setMockFiles(['/path/to/other.js', '/path/to/src/other.js']);54 expect(55 await transformAsync('import other from "../other";', options)56 ).toHaveProperty('code', 'import other from "../other.js";');57 });58 it('finds files in child directory', async () => {59 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/child/other.js']);60 expect(61 await transformAsync('import other from "./child/other";', options)62 ).toHaveProperty('code', 'import other from "./child/other.js";');63 });64 it('uses index file', async () => {65 fs.__setMockFiles(['/path/to/src/other/index.js']);66 expect(67 await transformAsync('import other from "./other";', options)68 ).toHaveProperty('code', 'import other from "./other/index.js";');69 });70 it('uses modulesToResolve', async () => {71 fs.__setMockFiles(['/path/to/src/other/index.js']);72 expect(73 await transformAsync(74 'import index from "instantsearch.js/es/widgets/index/index";\n' +75 'import root from "instantsearch.js";\n' +76 'import other from "different-module/other";',77 options78 )79 ).toHaveProperty(80 'code',81 'import index from "instantsearch.js/es/widgets/index/index.js";\n' +82 'import root from "instantsearch.js";\n' +83 'import other from "different-module/other";'84 );85 });86 it('works with multiple imports', async () => {87 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);88 expect(89 await transformAsync(90 'import other from "./other";\nimport another from "./another";',91 options92 )93 ).toHaveProperty(94 'code',95 'import other from "./other.js";\nimport another from "./another.js";'96 );97 });98 it('works with export from', async () => {99 fs.__setMockFiles(['/path/to/src/other.js']);100 expect(101 await transformAsync('export * from "./other"', options)102 ).toHaveProperty('code', 'export * from "./other.js";');103 });104 it('ignores require()', async () => {105 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);106 expect(await transformAsync('require("./other");', options)).toHaveProperty(107 'code',108 'require("./other");'109 );110 });111 it('ignores other function calls', async () => {112 fs.__setMockFiles(['/path/to/src/other.js']);113 expect(114 await transformAsync('requireOOPS("./other");', options)115 ).toHaveProperty('code', 'requireOOPS("./other");');116 });117 it('errors if file not found', async () => {118 fs.__setMockFiles([]);119 await expect(() =>120 transformAsync('import other from "./other";', options)121 ).rejects.toThrow(122 '/path/to/src/file.js: import for "./other" could not be resolved'123 );124 });125 it('errors if module file not found', async () => {126 fs.__setMockFiles([]);127 await expect(() =>128 transformAsync(129 'import other from "instantsearch.js/non-existing-folder-this-can-never-exist/qsdf/gh/jklm";',130 options131 )132 ).rejects.toThrow(133 "/path/to/src/file.js: Cannot find module 'instantsearch.js/non-existing-folder-this-can-never-exist/qsdf/gh/jklm' from 'scripts/babel/extension-resolver.js'"134 );135 });...
extension-resolver-test.js
Source:extension-resolver-test.js
...24 await transformAsync('import path from "path";', options)25 ).toHaveProperty('code', 'import path from "path";');26 });27 it('finds .js files', async () => {28 fs.__setMockFiles([29 '/path/to/src/other.js',30 '/path/to/src/other.ts',31 '/path/to/src/other.tsx',32 ]);33 expect(34 await transformAsync('import other from "./other";', options)35 ).toHaveProperty('code', 'import other from "./other.js";');36 });37 it('finds .ts files', async () => {38 fs.__setMockFiles(['/path/to/src/other.ts', '/path/to/src/other.tsx']);39 expect(40 await transformAsync('import other from "./other";', options)41 ).toHaveProperty('code', 'import other from "./other.js";');42 });43 it('finds .tsx files', async () => {44 fs.__setMockFiles(['/path/to/src/other.tsx']);45 expect(46 await transformAsync('import other from "./other";', options)47 ).toHaveProperty('code', 'import other from "./other.js";');48 });49 it('finds files in parent directory', async () => {50 fs.__setMockFiles(['/path/to/other.js', '/path/to/src/other.js']);51 expect(52 await transformAsync('import other from "../other";', options)53 ).toHaveProperty('code', 'import other from "../other.js";');54 });55 it('finds files in child directory', async () => {56 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/child/other.js']);57 expect(58 await transformAsync('import other from "./child/other";', options)59 ).toHaveProperty('code', 'import other from "./child/other.js";');60 });61 it('uses index file', async () => {62 fs.__setMockFiles(['/path/to/src/other/index.js']);63 expect(64 await transformAsync('import other from "./other";', options)65 ).toHaveProperty('code', 'import other from "./other/index.js";');66 });67 it('works with multiple imports', async () => {68 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);69 expect(70 await transformAsync(71 'import other from "./other";\nimport another from "./another";',72 options73 )74 ).toHaveProperty(75 'code',76 'import other from "./other.js";\nimport another from "./another.js";'77 );78 });79 it('works with export from', async () => {80 fs.__setMockFiles(['/path/to/src/other.js']);81 expect(82 await transformAsync('export * from "./other"', options)83 ).toHaveProperty('code', 'export * from "./other.js";');84 });85 it('ignores require()', async () => {86 fs.__setMockFiles(['/path/to/src/other.js', '/path/to/src/another.js']);87 expect(await transformAsync('require("./other");', options)).toHaveProperty(88 'code',89 'require("./other");'90 );91 });92 it('ignores other function calls', async () => {93 fs.__setMockFiles(['/path/to/src/other.js']);94 expect(95 await transformAsync('requireOOPS("./other");', options)96 ).toHaveProperty('code', 'requireOOPS("./other");');97 });98 it('leaves as-is if file not found', async () => {99 fs.__setMockFiles([]);100 await expect(() =>101 transformAsync('import other from "./other";', options)102 ).rejects.toThrow(103 '/path/to/src/file.js: local import for "./other" could not be resolved'104 );105 });...
fs-extra.js
Source:fs-extra.js
...3// what the files on the "mock" filesystem should look like when any of the4// `fs` APIs are used.5let mockFiles = Object.create(null);6// eslint-disable-next-line no-underscore-dangle7function __setMockFiles(newMockFiles) {8 mockFiles = newMockFiles;9}10// A custom version of `readdirSync` that reads from the special mocked out11// file list set via __setMockFiles12const readFile = async (filePath) => mockFiles[filePath];13const readFileSync = (filePath = '') => mockFiles[filePath];14const existsSync = (filePath) => !!mockFiles[filePath];15const lstatSync = (filePath) => ({16 isFile: () => !!mockFiles[filePath],17});18// eslint-disable-next-line no-underscore-dangle19fs.__setMockFiles = __setMockFiles;20fs.readFile = readFile;21fs.readFileSync = readFileSync;...
messagesMock.js
Source:messagesMock.js
...4// This is a custom function that our tests can use during setup to specify5// what the files on the "mock" filesystem should look like when any of the6// `fs` APIs are used.7let mockFiles = Object.create(null);8function __setMockFiles(newMockFiles) {9 mockFiles = Object.create(null);10 for (const file in newMockFiles) {11 const dir = path.dirname(file);12 if (!mockFiles[dir]) {13 mockFiles[dir] = [];14 }15 mockFiles[dir].push(path.basename(file));16 }17}18// A custom version of `readdirSync` that reads from the special mocked out19// file list set via __setMockFiles20function readdirSync(directoryPath) {21 return mockFiles[directoryPath] || [];22}...
fs.js
Source:fs.js
...3// what the files on the "mock" filesystem should look like when any of the4// `fs` APIs are used.5let mockFiles = Object.create(null);6// eslint-disable-next-line no-underscore-dangle7function __setMockFiles(newMockFiles) {8 mockFiles = newMockFiles;9}10// A custom version of `readdirSync` that reads from the special mocked out11// file list set via __setMockFiles12const readFileSync = (filePath = '') => mockFiles[filePath];13const existsSync = (filePath) => !!mockFiles[filePath];14const lstatSync = (filePath) => ({15 isFile: () => !!mockFiles[filePath],16});17// eslint-disable-next-line no-underscore-dangle18fs.__setMockFiles = __setMockFiles;19fs.readFileSync = readFileSync;20fs.existsSync = existsSync;21fs.lstatSync = lstatSync;...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!