|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import * as path from 'path'; |
| 9 | +import {wrap} from 'jest-snapshot-serializer-raw'; |
| 10 | +import runJest from '../runJest'; |
| 11 | +import {cleanup, extractSummary, writeFiles} from '../Utils'; |
| 12 | + |
| 13 | +const DIR = path.resolve(__dirname, '../jest.config.ts'); |
| 14 | + |
| 15 | +beforeEach(() => cleanup(DIR)); |
| 16 | +afterAll(() => cleanup(DIR)); |
| 17 | + |
| 18 | +test('works with jest.config.ts', () => { |
| 19 | + writeFiles(DIR, { |
| 20 | + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, |
| 21 | + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};`, |
| 22 | + 'package.json': '{}', |
| 23 | + }); |
| 24 | + |
| 25 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); |
| 26 | + const {rest, summary} = extractSummary(stderr); |
| 27 | + expect(exitCode).toBe(0); |
| 28 | + expect(wrap(rest)).toMatchSnapshot(); |
| 29 | + expect(wrap(summary)).toMatchSnapshot(); |
| 30 | +}); |
| 31 | + |
| 32 | +test('traverses directory tree up until it finds jest.config', () => { |
| 33 | + writeFiles(DIR, { |
| 34 | + '__tests__/a-giraffe.js': ` |
| 35 | + const slash = require('slash'); |
| 36 | + test('giraffe', () => expect(1).toBe(1)); |
| 37 | + test('abc', () => console.log(slash(process.cwd()))); |
| 38 | + `, |
| 39 | + 'jest.config.ts': `export default {testEnvironment: 'jest-environment-node', testRegex: '.*-giraffe.js'};`, |
| 40 | + 'package.json': '{}', |
| 41 | + 'some/nested/directory/file.js': '// nothing special', |
| 42 | + }); |
| 43 | + |
| 44 | + const {stderr, exitCode, stdout} = runJest( |
| 45 | + path.join(DIR, 'some', 'nested', 'directory'), |
| 46 | + ['-w=1', '--ci=false'], |
| 47 | + {skipPkgJsonCheck: true}, |
| 48 | + ); |
| 49 | + |
| 50 | + // Snapshot the console.loged `process.cwd()` and make sure it stays the same |
| 51 | + expect( |
| 52 | + wrap(stdout.replace(/^\W+(.*)e2e/gm, '<<REPLACED>>')), |
| 53 | + ).toMatchSnapshot(); |
| 54 | + |
| 55 | + const {rest, summary} = extractSummary(stderr); |
| 56 | + expect(exitCode).toBe(0); |
| 57 | + expect(wrap(rest)).toMatchSnapshot(); |
| 58 | + expect(wrap(summary)).toMatchSnapshot(); |
| 59 | +}); |
| 60 | + |
| 61 | +test('it does type check the config', () => { |
| 62 | + writeFiles(DIR, { |
| 63 | + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, |
| 64 | + 'jest.config.ts': `export default { testTimeout: "10000" }`, |
| 65 | + 'package.json': '{}', |
| 66 | + }); |
| 67 | + |
| 68 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); |
| 69 | + expect(stderr).toMatch('must be of type'); |
| 70 | + expect(exitCode).toBe(1); |
| 71 | +}); |
| 72 | + |
| 73 | +test('invalid JS in jest.config.ts', () => { |
| 74 | + writeFiles(DIR, { |
| 75 | + '__tests__/a-giraffe.js': `test('giraffe', () => expect(1).toBe(1));`, |
| 76 | + 'jest.config.ts': `export default i'll break this file yo`, |
| 77 | + 'package.json': '{}', |
| 78 | + }); |
| 79 | + |
| 80 | + const {stderr, exitCode} = runJest(DIR, ['-w=1', '--ci=false']); |
| 81 | + expect(stderr).toMatch('TSError: ⨯ Unable to compile TypeScript:'); |
| 82 | + expect(exitCode).toBe(1); |
| 83 | +}); |
0 commit comments