|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-present, Facebook, Inc. 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 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +const path = require('path'); |
| 13 | +const skipOnWindows = require('../../scripts/skip_on_windows'); |
| 14 | +const {extractSummary, cleanup, writeFiles} = require('../utils'); |
| 15 | +const runJest = require('../runJest'); |
| 16 | + |
| 17 | +const DIR = path.resolve(__dirname, '../execute-tests-once-in-mpr'); |
| 18 | + |
| 19 | +skipOnWindows.suite(); |
| 20 | + |
| 21 | +beforeEach(() => cleanup(DIR)); |
| 22 | +afterAll(() => cleanup(DIR)); |
| 23 | + |
| 24 | +test('Tests are executed only once even in an MPR', () => { |
| 25 | + // Make a global config that ignores all sub-projects. |
| 26 | + const config = { |
| 27 | + jest: { |
| 28 | + projects: ['<rootDir>', '<rootDir>/foo/*/'], |
| 29 | + testPathIgnorePatterns: ['/foo/'], |
| 30 | + testRegex: /my-test-.*\.js/.source, |
| 31 | + }, |
| 32 | + }; |
| 33 | + |
| 34 | + // Make a child config with a special regexp to ensure we execute the tests. |
| 35 | + const childConfig = { |
| 36 | + jest: { |
| 37 | + testRegex: /my-test-.*\.js/.source, |
| 38 | + }, |
| 39 | + }; |
| 40 | + |
| 41 | + /* eslint-disable sort-keys */ |
| 42 | + writeFiles(DIR, { |
| 43 | + 'foo/folder/my-test-bar.js': `test('bar', () => console.log('Bar!'));`, |
| 44 | + 'foo/folder/package.json': JSON.stringify(childConfig, null, 2), |
| 45 | + |
| 46 | + 'foo/directory/my-test-baz.js': `test('baz', () => console.log('Baz!'));`, |
| 47 | + 'foo/directory/package.json': JSON.stringify(childConfig, null, 2), |
| 48 | + |
| 49 | + 'foo/whatever/my-test-qux.js': `test('qux', () => console.log('Qux!'));`, |
| 50 | + 'foo/whatever/package.json': JSON.stringify(childConfig, null, 2), |
| 51 | + |
| 52 | + 'package.json': JSON.stringify(config, null, 2), |
| 53 | + }); |
| 54 | + /* eslint-enable sort-keys */ |
| 55 | + |
| 56 | + const {stderr, status} = runJest(DIR, ['foo/folder/my-test-bar.js']); |
| 57 | + |
| 58 | + expect(status).toBe(0); |
| 59 | + |
| 60 | + const {rest, summary} = extractSummary(stderr); |
| 61 | + |
| 62 | + // We have only one test passed, so total should equal to one, despite we have |
| 63 | + // three projects. |
| 64 | + expect(rest).toMatchSnapshot(); |
| 65 | + expect(summary).toMatch(/1 total/); |
| 66 | +}); |
0 commit comments