How to use joinForPattern method in Jest

Best JavaScript code snippet using jest

normalize.test.js

Source:normalize.test.js Github

copy

Full Screen

...28});29// Windows uses backslashes for path separators, which need to be escaped in30// regular expressions. This little helper function helps us generate the31// expected strings for checking path patterns.32function joinForPattern() {33 return Array.prototype.join.call(34 arguments,35 utils.escapeStrForRegex(path.sep),36 );37}38beforeEach(() => {39 root = path.resolve('/');40 expectedPathFooBar = path.join(root, 'root', 'path', 'foo', 'bar', 'baz');41 expectedPathFooQux = path.join(root, 'root', 'path', 'foo', 'qux', 'quux');42 expectedPathAbs = path.join(root, 'an', 'abs', 'path');43 expectedPathAbsAnother = path.join(root, 'another', 'abs', 'path');44 require('jest-resolve').findNodeModule = findNodeModule;45});46it('picks a name based on the rootDir', () => {47 const rootDir = '/root/path/foo';48 const expected = crypto49 .createHash('md5')50 .update('/root/path/foo')51 .digest('hex');52 expect(53 normalize(54 {55 rootDir,56 },57 {},58 ).options.name,59 ).toBe(expected);60});61it('keeps custom names based on the rootDir', () => {62 expect(63 normalize(64 {65 name: 'custom-name',66 rootDir: '/root/path/foo',67 },68 {},69 ).options.name,70 ).toBe('custom-name');71});72it('sets coverageReporters correctly when argv.json is set', () => {73 expect(74 normalize(75 {76 rootDir: '/root/path/foo',77 },78 {79 json: true,80 },81 ).options.coverageReporters,82 ).toEqual(['json', 'lcov', 'clover']);83});84describe('rootDir', () => {85 it('throws if the options is missing a rootDir property', () => {86 expect(() => {87 normalize({}, {});88 }).toThrowErrorMatchingSnapshot();89 });90});91describe('automock', () => {92 it('falsy automock is not overwritten', () => {93 const consoleWarn = console.warn;94 console.warn = jest.fn();95 const {options} = normalize(96 {97 automock: false,98 rootDir: '/root/path/foo',99 },100 {},101 );102 expect(options.automock).toBe(false);103 console.warn = consoleWarn;104 });105});106describe('browser', () => {107 it('falsy browser is not overwritten', () => {108 const {options} = normalize(109 {110 browser: true,111 rootDir: '/root/path/foo',112 },113 {},114 );115 expect(options.browser).toBe(true);116 });117});118describe('collectCoverageOnlyFrom', () => {119 it('normalizes all paths relative to rootDir', () => {120 const {options} = normalize(121 {122 collectCoverageOnlyFrom: {123 'bar/baz': true,124 'qux/quux/': true,125 },126 rootDir: '/root/path/foo/',127 },128 {},129 );130 const expected = {};131 expected[expectedPathFooBar] = true;132 expected[expectedPathFooQux] = true;133 expect(options.collectCoverageOnlyFrom).toEqual(expected);134 });135 it('does not change absolute paths', () => {136 const {options} = normalize(137 {138 collectCoverageOnlyFrom: {139 '/an/abs/path': true,140 '/another/abs/path': true,141 },142 rootDir: '/root/path/foo',143 },144 {},145 );146 const expected = {};147 expected[expectedPathAbs] = true;148 expected[expectedPathAbsAnother] = true;149 expect(options.collectCoverageOnlyFrom).toEqual(expected);150 });151 it('substitutes <rootDir> tokens', () => {152 const {options} = normalize(153 {154 collectCoverageOnlyFrom: {155 '<rootDir>/bar/baz': true,156 },157 rootDir: '/root/path/foo',158 },159 {},160 );161 const expected = {};162 expected[expectedPathFooBar] = true;163 expect(options.collectCoverageOnlyFrom).toEqual(expected);164 });165});166function testPathArray(key) {167 it('normalizes all paths relative to rootDir', () => {168 const {options} = normalize(169 {170 [key]: ['bar/baz', 'qux/quux/'],171 rootDir: '/root/path/foo',172 },173 {},174 );175 expect(options[key]).toEqual([expectedPathFooBar, expectedPathFooQux]);176 });177 it('does not change absolute paths', () => {178 const {options} = normalize(179 {180 [key]: ['/an/abs/path', '/another/abs/path'],181 rootDir: '/root/path/foo',182 },183 {},184 );185 expect(options[key]).toEqual([expectedPathAbs, expectedPathAbsAnother]);186 });187 it('substitutes <rootDir> tokens', () => {188 const {options} = normalize(189 {190 [key]: ['<rootDir>/bar/baz'],191 rootDir: '/root/path/foo',192 },193 {},194 );195 expect(options[key]).toEqual([expectedPathFooBar]);196 });197}198describe('roots', () => {199 testPathArray('roots');200});201describe('transform', () => {202 let Resolver;203 beforeEach(() => {204 Resolver = require('jest-resolve');205 Resolver.findNodeModule = jest.fn(name => name);206 });207 it('normalizes the path', () => {208 const {options} = normalize(209 {210 rootDir: '/root/',211 transform: {212 [DEFAULT_CSS_PATTERN]: '<rootDir>/node_modules/jest-regex-util',213 [DEFAULT_JS_PATTERN]: 'babel-jest',214 'abs-path': '/qux/quux',215 },216 },217 {},218 );219 expect(options.transform).toEqual([220 [DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'],221 [DEFAULT_JS_PATTERN, 'babel-jest'],222 ['abs-path', '/qux/quux'],223 ]);224 });225});226describe('haste', () => {227 let Resolver;228 beforeEach(() => {229 Resolver = require('jest-resolve');230 Resolver.findNodeModule = jest.fn(name => name);231 });232 it('normalizes the path for hasteImplModulePath', () => {233 const {options} = normalize(234 {235 haste: {236 hasteImplModulePath: '<rootDir>/haste_impl.js',237 },238 rootDir: '/root/',239 },240 {},241 );242 expect(options.haste).toEqual({243 hasteImplModulePath: '/root/haste_impl.js',244 });245 });246});247describe('setupTestFrameworkScriptFile', () => {248 let Resolver;249 beforeEach(() => {250 Resolver = require('jest-resolve');251 Resolver.findNodeModule = jest.fn(252 name =>253 name.startsWith('/') ? name : '/root/path/foo' + path.sep + name,254 );255 });256 it('normalizes the path according to rootDir', () => {257 const {options} = normalize(258 {259 rootDir: '/root/path/foo',260 setupTestFrameworkScriptFile: 'bar/baz',261 },262 {},263 );264 expect(options.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar);265 });266 it('does not change absolute paths', () => {267 const {options} = normalize(268 {269 rootDir: '/root/path/foo',270 setupTestFrameworkScriptFile: '/an/abs/path',271 },272 {},273 );274 expect(options.setupTestFrameworkScriptFile).toEqual(expectedPathAbs);275 });276 it('substitutes <rootDir> tokens', () => {277 const {options} = normalize(278 {279 rootDir: '/root/path/foo',280 setupTestFrameworkScriptFile: '<rootDir>/bar/baz',281 },282 {},283 );284 expect(options.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar);285 });286});287describe('coveragePathIgnorePatterns', () => {288 it('does not normalize paths relative to rootDir', () => {289 // This is a list of patterns, so we can't assume any of them are290 // directories291 const {options} = normalize(292 {293 coveragePathIgnorePatterns: ['bar/baz', 'qux/quux'],294 rootDir: '/root/path/foo',295 },296 {},297 );298 expect(options.coveragePathIgnorePatterns).toEqual([299 joinForPattern('bar', 'baz'),300 joinForPattern('qux', 'quux'),301 ]);302 });303 it('does not normalize trailing slashes', () => {304 // This is a list of patterns, so we can't assume any of them are305 // directories306 const {options} = normalize(307 {308 coveragePathIgnorePatterns: ['bar/baz', 'qux/quux/'],309 rootDir: '/root/path/foo',310 },311 {},312 );313 expect(options.coveragePathIgnorePatterns).toEqual([314 joinForPattern('bar', 'baz'),315 joinForPattern('qux', 'quux', ''),316 ]);317 });318 it('substitutes <rootDir> tokens', () => {319 const {options} = normalize(320 {321 coveragePathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],322 rootDir: '/root/path/foo',323 },324 {},325 );326 expect(options.coveragePathIgnorePatterns).toEqual([327 'hasNoToken',328 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),329 ]);330 });331});332describe('watchPathIgnorePatterns', () => {333 it('does not normalize paths relative to rootDir', () => {334 // This is a list of patterns, so we can't assume any of them are335 // directories336 const {options} = normalize(337 {338 rootDir: '/root/path/foo',339 watchPathIgnorePatterns: ['bar/baz', 'qux/quux'],340 },341 {},342 );343 expect(options.watchPathIgnorePatterns).toEqual([344 joinForPattern('bar', 'baz'),345 joinForPattern('qux', 'quux'),346 ]);347 });348 it('does not normalize trailing slashes', () => {349 // This is a list of patterns, so we can't assume any of them are350 // directories351 const {options} = normalize(352 {353 rootDir: '/root/path/foo',354 watchPathIgnorePatterns: ['bar/baz', 'qux/quux/'],355 },356 {},357 );358 expect(options.watchPathIgnorePatterns).toEqual([359 joinForPattern('bar', 'baz'),360 joinForPattern('qux', 'quux', ''),361 ]);362 });363 it('substitutes <rootDir> tokens', () => {364 const {options} = normalize(365 {366 rootDir: '/root/path/foo',367 watchPathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],368 },369 {},370 );371 expect(options.watchPathIgnorePatterns).toEqual([372 'hasNoToken',373 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),374 ]);375 });376});377describe('testPathIgnorePatterns', () => {378 it('does not normalize paths relative to rootDir', () => {379 // This is a list of patterns, so we can't assume any of them are380 // directories381 const {options} = normalize(382 {383 rootDir: '/root/path/foo',384 testPathIgnorePatterns: ['bar/baz', 'qux/quux'],385 },386 {},387 );388 expect(options.testPathIgnorePatterns).toEqual([389 joinForPattern('bar', 'baz'),390 joinForPattern('qux', 'quux'),391 ]);392 });393 it('does not normalize trailing slashes', () => {394 // This is a list of patterns, so we can't assume any of them are395 // directories396 const {options} = normalize(397 {398 rootDir: '/root/path/foo',399 testPathIgnorePatterns: ['bar/baz', 'qux/quux/'],400 },401 {},402 );403 expect(options.testPathIgnorePatterns).toEqual([404 joinForPattern('bar', 'baz'),405 joinForPattern('qux', 'quux', ''),406 ]);407 });408 it('substitutes <rootDir> tokens', () => {409 const {options} = normalize(410 {411 rootDir: '/root/path/foo',412 testPathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],413 },414 {},415 );416 expect(options.testPathIgnorePatterns).toEqual([417 'hasNoToken',418 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),419 ]);420 });421});422describe('modulePathIgnorePatterns', () => {423 it('does not normalize paths relative to rootDir', () => {424 // This is a list of patterns, so we can't assume any of them are425 // directories426 const {options} = normalize(427 {428 modulePathIgnorePatterns: ['bar/baz', 'qux/quux'],429 rootDir: '/root/path/foo',430 },431 {},432 );433 expect(options.modulePathIgnorePatterns).toEqual([434 joinForPattern('bar', 'baz'),435 joinForPattern('qux', 'quux'),436 ]);437 });438 it('does not normalize trailing slashes', () => {439 // This is a list of patterns, so we can't assume any of them are440 // directories441 const {options} = normalize(442 {443 modulePathIgnorePatterns: ['bar/baz', 'qux/quux/'],444 rootDir: '/root/path/foo',445 },446 {},447 );448 expect(options.modulePathIgnorePatterns).toEqual([449 joinForPattern('bar', 'baz'),450 joinForPattern('qux', 'quux', ''),451 ]);452 });453 it('substitutes <rootDir> tokens', () => {454 const {options} = normalize(455 {456 modulePathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],457 rootDir: '/root/path/foo',458 },459 {},460 );461 expect(options.modulePathIgnorePatterns).toEqual([462 'hasNoToken',463 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),464 ]);465 });466});467describe('testRunner', () => {468 it('defaults to Jasmine 2', () => {469 const {options} = normalize(470 {471 rootDir: '/root/path/foo',472 },473 {},474 );475 expect(options.testRunner).toMatch('jasmine2');476 });477 it('is overwritten by argv', () => {478 const Resolver = require('jest-resolve');479 Resolver.findNodeModule = jest.fn(name => name);480 const {options} = normalize(481 {482 rootDir: '/root/path/foo',483 },484 {485 testRunner: 'jasmine1',486 },487 );488 expect(options.testRunner).toBe('jasmine1');489 });490});491describe('coverageDirectory', () => {492 it('defaults to <rootDir>/coverage', () => {493 const {options} = normalize(494 {495 rootDir: '/root/path/foo',496 },497 {},498 );499 expect(options.coverageDirectory).toBe('/root/path/foo/coverage');500 });501});502describe('testEnvironment', () => {503 let Resolver;504 beforeEach(() => {505 Resolver = require('jest-resolve');506 Resolver.findNodeModule = jest.fn(name => {507 if (name === 'jsdom') {508 return 'node_modules/jsdom';509 }510 if (name === 'jest-environment-jsdom') {511 return 'node_modules/jest-environment-jsdom';512 }513 return findNodeModule(name);514 });515 });516 it('resolves to an environment and prefers jest-environment-`name`', () => {517 const {options} = normalize(518 {519 rootDir: '/root',520 testEnvironment: 'jsdom',521 },522 {},523 );524 expect(options.testEnvironment).toEqual(525 'node_modules/jest-environment-jsdom',526 );527 });528 it('throws on invalid environment names', () => {529 expect(() =>530 normalize(531 {532 rootDir: '/root',533 testEnvironment: 'phantom',534 },535 {},536 ),537 ).toThrowErrorMatchingSnapshot();538 });539});540describe('babel-jest', () => {541 let Resolver;542 beforeEach(() => {543 Resolver = require('jest-resolve');544 Resolver.findNodeModule = jest.fn(545 name => path.sep + 'node_modules' + path.sep + name,546 );547 });548 it('correctly identifies and uses babel-jest', () => {549 const {options} = normalize(550 {551 rootDir: '/root',552 },553 {},554 );555 expect(options.transform[0][0]).toBe(DEFAULT_JS_PATTERN);556 expect(options.transform[0][1]).toEqual(557 path.sep + 'node_modules' + path.sep + 'babel-jest',558 );559 expect(options.setupFiles).toEqual([560 path.sep +561 'node_modules' +562 path.sep +563 'regenerator-runtime' +564 path.sep +565 'runtime',566 ]);567 });568 it('uses babel-jest if babel-jest is explicitly specified in a custom transform options', () => {569 const customJSPattern = '^.+\\.js$';570 const {options} = normalize(571 {572 rootDir: '/root',573 transform: {574 [customJSPattern]: 'babel-jest',575 },576 },577 {},578 );579 expect(options.transform[0][0]).toBe(customJSPattern);580 expect(options.transform[0][1]).toEqual('/node_modules/babel-jest');581 expect(options.setupFiles).toEqual([582 path.sep +583 'node_modules' +584 path.sep +585 'regenerator-runtime' +586 path.sep +587 'runtime',588 ]);589 });590 it(`doesn't use babel-jest if its not available`, () => {591 Resolver.findNodeModule = findNodeModule;592 const {options} = normalize(593 {594 rootDir: '/root',595 },596 {},597 );598 expect(options.transform).toEqual(undefined);599 expect(options.setupFiles).toEqual([]);600 });601 it('uses regenerator if babel-jest is explicitly specified', () => {602 const ROOT_DIR = '<rootDir>' + path.sep;603 const {options} = normalize(604 {605 rootDir: '/root',606 transform: {607 [DEFAULT_JS_PATTERN]:608 ROOT_DIR + Resolver.findNodeModule('babel-jest'),609 },610 },611 {},612 );613 expect(options.setupFiles).toEqual([614 path.sep +615 'node_modules' +616 path.sep +617 'regenerator-runtime' +618 path.sep +619 'runtime',620 ]);621 });622});623describe('Upgrade help', () => {624 let consoleWarn;625 beforeEach(() => {626 consoleWarn = console.warn;627 console.warn = jest.fn();628 const Resolver = require('jest-resolve');629 Resolver.findNodeModule = jest.fn(name => {630 if (name == 'bar/baz') {631 return '/node_modules/bar/baz';632 }633 return findNodeModule(name);634 });635 });636 afterEach(() => {637 console.warn = consoleWarn;638 });639 it('logs a warning when `scriptPreprocessor` and/or `preprocessorIgnorePatterns` are used', () => {640 const {options: options, hasDeprecationWarnings} = normalize(641 {642 preprocessorIgnorePatterns: ['bar/baz', 'qux/quux'],643 rootDir: '/root/path/foo',644 scriptPreprocessor: 'bar/baz',645 },646 {},647 );648 expect(options.transform).toEqual([['.*', '/node_modules/bar/baz']]);649 expect(options.transformIgnorePatterns).toEqual([650 joinForPattern('bar', 'baz'),651 joinForPattern('qux', 'quux'),652 ]);653 expect(options.scriptPreprocessor).toBe(undefined);654 expect(options.preprocessorIgnorePatterns).toBe(undefined);655 expect(hasDeprecationWarnings).toBeTruthy();656 expect(console.warn.mock.calls[0][0]).toMatchSnapshot();657 });658});659describe('testMatch', () => {660 it('testMatch default not applied if testRegex is set', () => {661 const {options} = normalize(662 {663 rootDir: '/root',664 testRegex: '.*',665 },...

Full Screen

Full Screen

normalize-test.js

Source:normalize-test.js Github

copy

Full Screen

...22let expectedPathAbsAnother;23// Windows uses backslashes for path separators, which need to be escaped in24// regular expressions. This little helper function helps us generate the25// expected strings for checking path patterns.26function joinForPattern() {27 return Array.prototype.join.call(28 arguments,29 utils.escapeStrForRegex(path.sep),30 );31}32beforeEach(() => {33 root = path.resolve('/');34 expectedPathFooBar = path.join(root, 'root', 'path', 'foo', 'bar', 'baz');35 expectedPathFooQux = path.join(root, 'root', 'path', 'foo', 'qux', 'quux');36 expectedPathAbs = path.join(root, 'an', 'abs', 'path');37 expectedPathAbsAnother = path.join(root, 'another', 'abs', 'path');38});39it('picks a name based on the rootDir', () => {40 const rootDir = '/root/path/foo';41 const expected = crypto42 .createHash('md5')43 .update('/root/path/foo')44 .digest('hex');45 expect(46 normalize({47 rootDir,48 }).config.name,49 ).toBe(expected);50});51it('keeps custom names based on the rootDir', () => {52 expect(53 normalize({54 name: 'custom-name',55 rootDir: '/root/path/foo',56 }).config.name,57 ).toBe('custom-name');58});59it('sets coverageReporters correctly when argv.json is set', () => {60 expect(61 normalize(62 {63 rootDir: '/root/path/foo',64 },65 {66 json: true,67 },68 ).config.coverageReporters,69 ).toEqual(['json', 'lcov', 'clover']);70});71describe('rootDir', () => {72 it('throws if the config is missing a rootDir property', () => {73 expect(() => {74 normalize({});75 }).toThrowErrorMatchingSnapshot();76 });77});78describe('automock', () => {79 it('falsy automock is not overwritten', () => {80 const consoleWarn = console.warn;81 console.warn = jest.fn();82 const {config} = normalize({83 automock: false,84 rootDir: '/root/path/foo',85 });86 expect(config.automock).toBe(false);87 console.warn = consoleWarn;88 });89});90describe('browser', () => {91 it('falsy browser is not overwritten', () => {92 const {config} = normalize({93 browser: true,94 rootDir: '/root/path/foo',95 });96 expect(config.browser).toBe(true);97 });98});99describe('collectCoverageOnlyFrom', () => {100 it('normalizes all paths relative to rootDir', () => {101 const {config} = normalize(102 {103 collectCoverageOnlyFrom: {104 'bar/baz': true,105 'qux/quux/': true,106 },107 rootDir: '/root/path/foo/',108 },109 '/root/path',110 );111 const expected = {};112 expected[expectedPathFooBar] = true;113 expected[expectedPathFooQux] = true;114 expect(config.collectCoverageOnlyFrom).toEqual(expected);115 });116 it('does not change absolute paths', () => {117 const {config} = normalize({118 collectCoverageOnlyFrom: {119 '/an/abs/path': true,120 '/another/abs/path': true,121 },122 rootDir: '/root/path/foo',123 });124 const expected = {};125 expected[expectedPathAbs] = true;126 expected[expectedPathAbsAnother] = true;127 expect(config.collectCoverageOnlyFrom).toEqual(expected);128 });129 it('substitutes <rootDir> tokens', () => {130 const {config} = normalize({131 collectCoverageOnlyFrom: {132 '<rootDir>/bar/baz': true,133 },134 rootDir: '/root/path/foo',135 });136 const expected = {};137 expected[expectedPathFooBar] = true;138 expect(config.collectCoverageOnlyFrom).toEqual(expected);139 });140});141function testPathArray(key) {142 it('normalizes all paths relative to rootDir', () => {143 const {config} = normalize(144 {145 [key]: ['bar/baz', 'qux/quux/'],146 rootDir: '/root/path/foo',147 },148 '/root/path',149 );150 expect(config[key]).toEqual([expectedPathFooBar, expectedPathFooQux]);151 });152 it('does not change absolute paths', () => {153 const {config} = normalize({154 [key]: ['/an/abs/path', '/another/abs/path'],155 rootDir: '/root/path/foo',156 });157 expect(config[key]).toEqual([expectedPathAbs, expectedPathAbsAnother]);158 });159 it('substitutes <rootDir> tokens', () => {160 const {config} = normalize({161 [key]: ['<rootDir>/bar/baz'],162 rootDir: '/root/path/foo',163 });164 expect(config[key]).toEqual([expectedPathFooBar]);165 });166}167describe('roots', () => {168 testPathArray('roots');169});170describe('transform', () => {171 let Resolver;172 beforeEach(() => {173 Resolver = require('jest-resolve');174 Resolver.findNodeModule = jest.fn(name => name);175 });176 it('normalizes the path', () => {177 const {config} = normalize(178 {179 rootDir: '/root/',180 transform: {181 [DEFAULT_CSS_PATTERN]: '<rootDir>/node_modules/jest-regex-util',182 [DEFAULT_JS_PATTERN]: 'babel-jest',183 'abs-path': '/qux/quux',184 },185 },186 '/root/path',187 );188 expect(config.transform).toEqual([189 [DEFAULT_CSS_PATTERN, '/root/node_modules/jest-regex-util'],190 [DEFAULT_JS_PATTERN, 'babel-jest'],191 ['abs-path', '/qux/quux'],192 ]);193 });194});195describe('haste', () => {196 let Resolver;197 beforeEach(() => {198 Resolver = require('jest-resolve');199 Resolver.findNodeModule = jest.fn(name => name);200 });201 it('normalizes the path for hasteImplModulePath', () => {202 const {config} = normalize({203 haste: {204 hasteImplModulePath: '<rootDir>/hasteImpl.js',205 },206 rootDir: '/root/',207 });208 expect(config.haste).toEqual({209 hasteImplModulePath: '/root/hasteImpl.js',210 });211 });212});213describe('setupTestFrameworkScriptFile', () => {214 let Resolver;215 beforeEach(() => {216 Resolver = require('jest-resolve');217 Resolver.findNodeModule = jest.fn(218 name => name.startsWith('/') ? name : '/root/path/foo' + path.sep + name,219 );220 });221 it('normalizes the path according to rootDir', () => {222 const {config} = normalize(223 {224 rootDir: '/root/path/foo',225 setupTestFrameworkScriptFile: 'bar/baz',226 },227 '/root/path',228 );229 expect(config.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar);230 });231 it('does not change absolute paths', () => {232 const {config} = normalize({233 rootDir: '/root/path/foo',234 setupTestFrameworkScriptFile: '/an/abs/path',235 });236 expect(config.setupTestFrameworkScriptFile).toEqual(expectedPathAbs);237 });238 it('substitutes <rootDir> tokens', () => {239 const {config} = normalize({240 rootDir: '/root/path/foo',241 setupTestFrameworkScriptFile: '<rootDir>/bar/baz',242 });243 expect(config.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar);244 });245});246describe('coveragePathIgnorePatterns', () => {247 it('does not normalize paths relative to rootDir', () => {248 // This is a list of patterns, so we can't assume any of them are249 // directories250 const {config} = normalize(251 {252 coveragePathIgnorePatterns: ['bar/baz', 'qux/quux'],253 rootDir: '/root/path/foo',254 },255 '/root/path',256 );257 expect(config.coveragePathIgnorePatterns).toEqual([258 joinForPattern('bar', 'baz'),259 joinForPattern('qux', 'quux'),260 ]);261 });262 it('does not normalize trailing slashes', () => {263 // This is a list of patterns, so we can't assume any of them are264 // directories265 const {config} = normalize({266 coveragePathIgnorePatterns: ['bar/baz', 'qux/quux/'],267 rootDir: '/root/path/foo',268 });269 expect(config.coveragePathIgnorePatterns).toEqual([270 joinForPattern('bar', 'baz'),271 joinForPattern('qux', 'quux', ''),272 ]);273 });274 it('substitutes <rootDir> tokens', () => {275 const {config} = normalize({276 coveragePathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],277 rootDir: '/root/path/foo',278 });279 expect(config.coveragePathIgnorePatterns).toEqual([280 'hasNoToken',281 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),282 ]);283 });284});285describe('testPathIgnorePatterns', () => {286 it('does not normalize paths relative to rootDir', () => {287 // This is a list of patterns, so we can't assume any of them are288 // directories289 const {config} = normalize(290 {291 rootDir: '/root/path/foo',292 testPathIgnorePatterns: ['bar/baz', 'qux/quux'],293 },294 '/root/path',295 );296 expect(config.testPathIgnorePatterns).toEqual([297 joinForPattern('bar', 'baz'),298 joinForPattern('qux', 'quux'),299 ]);300 });301 it('does not normalize trailing slashes', () => {302 // This is a list of patterns, so we can't assume any of them are303 // directories304 const {config} = normalize({305 rootDir: '/root/path/foo',306 testPathIgnorePatterns: ['bar/baz', 'qux/quux/'],307 });308 expect(config.testPathIgnorePatterns).toEqual([309 joinForPattern('bar', 'baz'),310 joinForPattern('qux', 'quux', ''),311 ]);312 });313 it('substitutes <rootDir> tokens', () => {314 const {config} = normalize({315 rootDir: '/root/path/foo',316 testPathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],317 });318 expect(config.testPathIgnorePatterns).toEqual([319 'hasNoToken',320 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),321 ]);322 });323});324describe('modulePathIgnorePatterns', () => {325 it('does not normalize paths relative to rootDir', () => {326 // This is a list of patterns, so we can't assume any of them are327 // directories328 const {config} = normalize(329 {330 modulePathIgnorePatterns: ['bar/baz', 'qux/quux'],331 rootDir: '/root/path/foo',332 },333 '/root/path',334 );335 expect(config.modulePathIgnorePatterns).toEqual([336 joinForPattern('bar', 'baz'),337 joinForPattern('qux', 'quux'),338 ]);339 });340 it('does not normalize trailing slashes', () => {341 // This is a list of patterns, so we can't assume any of them are342 // directories343 const {config} = normalize({344 modulePathIgnorePatterns: ['bar/baz', 'qux/quux/'],345 rootDir: '/root/path/foo',346 });347 expect(config.modulePathIgnorePatterns).toEqual([348 joinForPattern('bar', 'baz'),349 joinForPattern('qux', 'quux', ''),350 ]);351 });352 it('substitutes <rootDir> tokens', () => {353 const {config} = normalize({354 modulePathIgnorePatterns: ['hasNoToken', '<rootDir>/hasAToken'],355 rootDir: '/root/path/foo',356 });357 expect(config.modulePathIgnorePatterns).toEqual([358 'hasNoToken',359 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),360 ]);361 });362});363describe('testRunner', () => {364 it('defaults to Jasmine 2', () => {365 const {config} = normalize({366 rootDir: '/root/path/foo',367 });368 expect(config.testRunner).toMatch('jasmine2');369 });370 it('can be changed to jasmine1', () => {371 const {config} = normalize({372 rootDir: '/root/path/foo',373 testRunner: 'jasmine1',374 });375 expect(config.testRunner).toMatch('jasmine1');376 });377 it('is overwritten by argv', () => {378 const {config} = normalize(379 {380 rootDir: '/root/path/foo',381 },382 {383 testRunner: 'jasmine1',384 },385 );386 expect(config.testRunner).toMatch('jasmine1');387 });388});389describe('testEnvironment', () => {390 let Resolver;391 beforeEach(() => {392 Resolver = require('jest-resolve');393 Resolver.findNodeModule = jest.fn(name => {394 if (name === 'jsdom') {395 return 'node_modules/jsdom';396 }397 if (name === 'jest-environment-jsdom') {398 return 'node_modules/jest-environment-jsdom';399 }400 return null;401 });402 });403 it('resolves to an environment and prefers jest-environment-`name`', () => {404 const {config} = normalize({405 rootDir: '/root',406 testEnvironment: 'jsdom',407 });408 expect(config.testEnvironment).toEqual(409 'node_modules/jest-environment-jsdom',410 );411 });412 it('throws on invalid environment names', () => {413 expect(() =>414 normalize({415 rootDir: '/root',416 testEnvironment: 'phantom',417 })).toThrowErrorMatchingSnapshot();418 });419});420describe('babel-jest', () => {421 let Resolver;422 beforeEach(() => {423 Resolver = require('jest-resolve');424 Resolver.findNodeModule = jest.fn(425 name => path.sep + 'node_modules' + path.sep + name,426 );427 });428 it('correctly identifies and uses babel-jest', () => {429 const {config} = normalize({430 rootDir: '/root',431 });432 expect(config.transform[0][0]).toBe(DEFAULT_JS_PATTERN);433 expect(config.transform[0][1]).toEqual(434 path.sep + 'node_modules' + path.sep + 'babel-jest',435 );436 expect(config.setupFiles).toEqual([437 path.sep +438 'node_modules' +439 path.sep +440 'regenerator-runtime' +441 path.sep +442 'runtime',443 ]);444 });445 it('uses babel-jest if babel-jest is explicitly specified in a custom transform config', () => {446 const customJSPattern = '^.+\\.js$';447 const {config} = normalize({448 rootDir: '/root',449 transform: {450 [customJSPattern]: 'babel-jest',451 },452 });453 expect(config.transform[0][0]).toBe(customJSPattern);454 expect(config.transform[0][1]).toEqual('/node_modules/babel-jest');455 expect(config.setupFiles).toEqual([456 path.sep +457 'node_modules' +458 path.sep +459 'regenerator-runtime' +460 path.sep +461 'runtime',462 ]);463 });464 it(`doesn't use babel-jest if its not available`, () => {465 Resolver.findNodeModule.mockImplementation(() => null);466 const {config} = normalize({467 rootDir: '/root',468 });469 expect(config.transform).toEqual(undefined);470 expect(config.setupFiles).toEqual([]);471 });472 it('uses regenerator if babel-jest is explicitly specified', () => {473 const ROOT_DIR = '<rootDir>' + path.sep;474 const {config} = normalize({475 rootDir: '/root',476 transform: {477 [DEFAULT_JS_PATTERN]: ROOT_DIR + Resolver.findNodeModule('babel-jest'),478 },479 });480 expect(config.setupFiles).toEqual([481 path.sep +482 'node_modules' +483 path.sep +484 'regenerator-runtime' +485 path.sep +486 'runtime',487 ]);488 });489});490describe('Upgrade help', () => {491 let consoleWarn;492 beforeEach(() => {493 consoleWarn = console.warn;494 console.warn = jest.fn();495 });496 afterEach(() => {497 console.warn = consoleWarn;498 });499 it('logs a warning when `scriptPreprocessor` and/or `preprocessorIgnorePatterns` are used', () => {500 const {config, hasDeprecationWarnings} = normalize({501 preprocessorIgnorePatterns: ['bar/baz', 'qux/quux'],502 rootDir: '/root/path/foo',503 scriptPreprocessor: 'bar/baz',504 });505 expect(config.transform).toEqual([['.*', '/node_modules/bar/baz']]);506 expect(config.transformIgnorePatterns).toEqual([507 joinForPattern('bar', 'baz'),508 joinForPattern('qux', 'quux'),509 ]);510 expect(config.scriptPreprocessor).toBe(undefined);511 expect(config.preprocessorIgnorePatterns).toBe(undefined);512 expect(hasDeprecationWarnings).toBeTruthy();513 expect(console.warn.mock.calls[0][0]).toMatchSnapshot();514 });515});516describe('testMatch', () => {517 it('testMatch default not applied if testRegex is set', () => {518 const {config} = normalize({519 rootDir: '/root',520 testRegex: '.*',521 });522 expect(config.testMatch.length).toBe(0);...

Full Screen

Full Screen

utils-normalizeConfig-test.js

Source:utils-normalizeConfig-test.js Github

copy

Full Screen

...19 var expectedPathAbsAnother;20 // Windows uses backslashes for path separators, which need to be escaped in21 // regular expressions. This little helper function helps us generate the22 // expected strings for checking path patterns.23 function joinForPattern() {24 return Array.prototype.join.call(25 arguments,26 utils.escapeStrForRegex(path.sep)27 );28 }29 beforeEach(function() {30 path = require('path');31 root = path.resolve('/');32 expectedPathFooBar = path.join(root, 'root', 'path', 'foo', 'bar', 'baz');33 expectedPathFooQux = path.join(root, 'root', 'path', 'foo', 'qux', 'quux');34 expectedPathAbs = path.join(root, 'an', 'abs', 'path');35 expectedPathAbsAnother = path.join(root, 'another', 'abs', 'path');36 utils = require('../utils');37 });38 it('throws when an invalid config option is passed in', function() {39 expect(function() {40 utils.normalizeConfig({41 rootDir: '/root/path/foo',42 thisIsAnInvalidConfigKey: 'with a value even!',43 });44 }).toThrow(new Error('Unknown config option: thisIsAnInvalidConfigKey'));45 });46 describe('rootDir', function() {47 it('throws if the config is missing a rootDir property', function() {48 expect(function() {49 utils.normalizeConfig({});50 }).toThrow(new Error('No rootDir config value found!'));51 });52 });53 describe('collectCoverageOnlyFrom', function() {54 it('normalizes all paths relative to rootDir', function() {55 var config = utils.normalizeConfig({56 rootDir: '/root/path/foo/',57 collectCoverageOnlyFrom: {58 'bar/baz': true,59 'qux/quux/': true,60 },61 }, '/root/path');62 var expected = {};63 expected[expectedPathFooBar] = true;64 expected[expectedPathFooQux] = true;65 expect(config.collectCoverageOnlyFrom).toEqual(expected);66 });67 it('does not change absolute paths', function() {68 var config = utils.normalizeConfig({69 rootDir: '/root/path/foo',70 collectCoverageOnlyFrom: {71 '/an/abs/path': true,72 '/another/abs/path': true,73 },74 });75 var expected = {};76 expected[expectedPathAbs] = true;77 expected[expectedPathAbsAnother] = true;78 expect(config.collectCoverageOnlyFrom).toEqual(expected);79 });80 it('substitutes <rootDir> tokens', function() {81 var config = utils.normalizeConfig({82 rootDir: '/root/path/foo',83 collectCoverageOnlyFrom: {84 '<rootDir>/bar/baz': true,85 },86 });87 var expected = {};88 expected[expectedPathFooBar] = true;89 expect(config.collectCoverageOnlyFrom).toEqual(expected);90 });91 });92 describe('testPathDirs', function() {93 it('normalizes all paths relative to rootDir', function() {94 var config = utils.normalizeConfig({95 rootDir: '/root/path/foo',96 testPathDirs: [97 'bar/baz',98 'qux/quux/',99 ],100 }, '/root/path');101 expect(config.testPathDirs).toEqual([102 expectedPathFooBar, expectedPathFooQux,103 ]);104 });105 it('does not change absolute paths', function() {106 var config = utils.normalizeConfig({107 rootDir: '/root/path/foo',108 testPathDirs: [109 '/an/abs/path',110 '/another/abs/path',111 ],112 });113 expect(config.testPathDirs).toEqual([114 expectedPathAbs, expectedPathAbsAnother,115 ]);116 });117 it('substitutes <rootDir> tokens', function() {118 var config = utils.normalizeConfig({119 rootDir: '/root/path/foo',120 testPathDirs: [121 '<rootDir>/bar/baz',122 ],123 });124 expect(config.testPathDirs).toEqual([expectedPathFooBar]);125 });126 });127 describe('scriptPreprocessor', function() {128 it('normalizes the path according to rootDir', function() {129 var config = utils.normalizeConfig({130 rootDir: '/root/path/foo',131 scriptPreprocessor: 'bar/baz',132 }, '/root/path');133 expect(config.scriptPreprocessor).toEqual(expectedPathFooBar);134 });135 it('does not change absolute paths', function() {136 var config = utils.normalizeConfig({137 rootDir: '/root/path/foo',138 scriptPreprocessor: '/an/abs/path',139 });140 expect(config.scriptPreprocessor).toEqual(expectedPathAbs);141 });142 it('substitutes <rootDir> tokens', function() {143 var config = utils.normalizeConfig({144 rootDir: '/root/path/foo',145 scriptPreprocessor: '<rootDir>/bar/baz',146 });147 expect(config.scriptPreprocessor).toEqual(expectedPathFooBar);148 });149 });150 describe('setupEnvScriptFile', function() {151 it('normalizes the path according to rootDir', function() {152 var config = utils.normalizeConfig({153 rootDir: '/root/path/foo',154 setupEnvScriptFile: 'bar/baz',155 }, '/root/path');156 expect(config.setupEnvScriptFile).toEqual(expectedPathFooBar);157 });158 it('does not change absolute paths', function() {159 var config = utils.normalizeConfig({160 rootDir: '/root/path/foo',161 setupEnvScriptFile: '/an/abs/path',162 });163 expect(config.setupEnvScriptFile).toEqual(expectedPathAbs);164 });165 it('substitutes <rootDir> tokens', function() {166 var config = utils.normalizeConfig({167 rootDir: '/root/path/foo',168 setupEnvScriptFile: '<rootDir>/bar/baz',169 });170 expect(config.setupEnvScriptFile).toEqual(expectedPathFooBar);171 });172 });173 describe('setupTestFrameworkScriptFile', function() {174 it('normalizes the path according to rootDir', function() {175 var config = utils.normalizeConfig({176 rootDir: '/root/path/foo',177 setupTestFrameworkScriptFile: 'bar/baz',178 }, '/root/path');179 expect(config.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar);180 });181 it('does not change absolute paths', function() {182 var config = utils.normalizeConfig({183 rootDir: '/root/path/foo',184 setupTestFrameworkScriptFile: '/an/abs/path',185 });186 expect(config.setupTestFrameworkScriptFile).toEqual(expectedPathAbs);187 });188 it('substitutes <rootDir> tokens', function() {189 var config = utils.normalizeConfig({190 rootDir: '/root/path/foo',191 setupTestFrameworkScriptFile: '<rootDir>/bar/baz',192 });193 expect(config.setupTestFrameworkScriptFile).toEqual(expectedPathFooBar);194 });195 });196 describe('testPathIgnorePatterns', function() {197 it('does not normalize paths relative to rootDir', function() {198 // This is a list of patterns, so we can't assume any of them are199 // directories200 var config = utils.normalizeConfig({201 rootDir: '/root/path/foo',202 testPathIgnorePatterns: [203 'bar/baz',204 'qux/quux',205 ],206 }, '/root/path');207 expect(config.testPathIgnorePatterns).toEqual([208 joinForPattern('bar', 'baz'),209 joinForPattern('qux', 'quux'),210 ]);211 });212 it('does not normalize trailing slashes', function() {213 // This is a list of patterns, so we can't assume any of them are214 // directories215 var config = utils.normalizeConfig({216 rootDir: '/root/path/foo',217 testPathIgnorePatterns: [218 'bar/baz',219 'qux/quux/',220 ],221 });222 expect(config.testPathIgnorePatterns).toEqual([223 joinForPattern('bar', 'baz'),224 joinForPattern('qux', 'quux', ''),225 ]);226 });227 it('substitutes <rootDir> tokens', function() {228 var config = utils.normalizeConfig({229 rootDir: '/root/path/foo',230 testPathIgnorePatterns: [231 'hasNoToken',232 '<rootDir>/hasAToken',233 ],234 });235 expect(config.testPathIgnorePatterns).toEqual([236 'hasNoToken',237 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),238 ]);239 });240 });241 describe('modulePathIgnorePatterns', function() {242 it('does not normalize paths relative to rootDir', function() {243 // This is a list of patterns, so we can't assume any of them are244 // directories245 var config = utils.normalizeConfig({246 rootDir: '/root/path/foo',247 modulePathIgnorePatterns: [248 'bar/baz',249 'qux/quux',250 ],251 }, '/root/path');252 expect(config.modulePathIgnorePatterns).toEqual([253 joinForPattern('bar', 'baz'),254 joinForPattern('qux', 'quux'),255 ]);256 });257 it('does not normalize trailing slashes', function() {258 // This is a list of patterns, so we can't assume any of them are259 // directories260 var config = utils.normalizeConfig({261 rootDir: '/root/path/foo',262 modulePathIgnorePatterns: [263 'bar/baz',264 'qux/quux/',265 ],266 });267 expect(config.modulePathIgnorePatterns).toEqual([268 joinForPattern('bar', 'baz'),269 joinForPattern('qux', 'quux', ''),270 ]);271 });272 it('substitutes <rootDir> tokens', function() {273 var config = utils.normalizeConfig({274 rootDir: '/root/path/foo',275 modulePathIgnorePatterns: [276 'hasNoToken',277 '<rootDir>/hasAToken',278 ],279 });280 expect(config.modulePathIgnorePatterns).toEqual([281 'hasNoToken',282 joinForPattern('', 'root', 'path', 'foo', 'hasAToken'),283 ]);284 });285 });...

Full Screen

Full Screen

Jest Testing Tutorial

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.

Chapters

  1. What is Jest Framework
  2. Advantages of Jest - Jest has 3,898,000 GitHub repositories, as mentioned on its official website. Learn what makes Jest special and why Jest has gained popularity among the testing and developer community.
  3. Jest Installation - All the prerequisites and set up steps needed to help you start Jest automation testing.
  4. Using Jest with NodeJS Project - Learn how to leverage Jest framework to automate testing using a NodeJS Project.
  5. Writing First Test for Jest Framework - Get started with code-based tutorial to help you write and execute your first Jest framework testing script.
  6. Jest Vocabulary - Learn the industry renowned and official jargons of the Jest framework by digging deep into the Jest vocabulary.
  7. Unit Testing with Jest - Step-by-step tutorial to help you execute unit testing with Jest framework.
  8. Jest Basics - Learn about the most pivotal and basic features which makes Jest special.
  9. Jest Parameterized Tests - Avoid code duplication and fasten automation testing with Jest using parameterized tests. Parameterization allows you to trigger the same test scenario over different test configurations by incorporating parameters.
  10. Jest Matchers - Enforce assertions better with the help of matchers. Matchers help you compare the actual output with the expected one. Here is an example to see if the object is acquired from the correct class or not. -

|<p>it('check_object_of_Car', () => {</p><p> expect(newCar()).toBeInstanceOf(Car);</p><p> });</p>| | :- |

  1. Jest Hooks: Setup and Teardown - Learn how to set up conditions which needs to be followed by the test execution and incorporate a tear down function to free resources after the execution is complete.
  2. Jest Code Coverage - Unsure there is no code left unchecked in your application. Jest gives a specific flag called --coverage to help you generate code coverage.
  3. HTML Report Generation - Learn how to create a comprehensive HTML report based on your Jest test execution.
  4. Testing React app using Jest Framework - Learn how to test your react web-application with Jest framework in this detailed Jest tutorial.
  5. Test using LambdaTest cloud Selenium Grid - Run your Jest testing script over LambdaTest cloud-based platform and leverage parallel testing to help trim down your test execution time.
  6. Snapshot Testing for React Front Ends - Capture screenshots of your react based web-application and compare them automatically for visual anomalies with the help of Jest tutorial.
  7. Bonus: Import ES modules with Jest - ES modules are also known as ECMAScript modules. Learn how to best use them by importing in your Jest testing scripts.
  8. Jest vs Mocha vs Jasmine - Learn the key differences between the most popular JavaScript-based testing frameworks i.e. Jest, Mocha, and Jasmine.
  9. Jest FAQs(Frequently Asked Questions) - Explore the most commonly asked questions around Jest framework, with their answers.

Run Jest 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