|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 {beforeEach, describe, expect, it, jest} from '@jest/globals'; |
| 9 | +import {makeProjectConfig} from '@jest/test-utils'; |
| 10 | +import type {Circus} from '@jest/types'; |
| 11 | +import {getState as getRunnerState, resetState} from '../../state'; |
| 12 | +import {makeDescribe, makeTest} from '../../utils'; |
| 13 | +import {collectTestsWithoutRunning} from '../jestAdapterInit'; |
| 14 | + |
| 15 | +jest.mock('../../state', () => { |
| 16 | + const actual = |
| 17 | + jest.requireActual<typeof import('../../state')>('../../state'); |
| 18 | + return {...actual, dispatch: jest.fn<typeof actual.dispatch>()}; |
| 19 | +}); |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + resetState(); |
| 23 | +}); |
| 24 | + |
| 25 | +const addTest = (name: string, parent: Circus.DescribeBlock) => { |
| 26 | + const test = makeTest( |
| 27 | + () => {}, |
| 28 | + undefined, |
| 29 | + false, |
| 30 | + name, |
| 31 | + parent, |
| 32 | + undefined, |
| 33 | + new Error(), |
| 34 | + false, |
| 35 | + ); |
| 36 | + parent.children.push(test); |
| 37 | +}; |
| 38 | + |
| 39 | +const collect = (testPath = '/test.js') => |
| 40 | + collectTestsWithoutRunning({config: makeProjectConfig(), testPath}); |
| 41 | + |
| 42 | +describe('collectTestsWithoutRunning', () => { |
| 43 | + it('collects flat tests with pending status', async () => { |
| 44 | + const root = getRunnerState().rootDescribeBlock; |
| 45 | + addTest('test one', root); |
| 46 | + addTest('test two', root); |
| 47 | + |
| 48 | + const result = await collect(); |
| 49 | + |
| 50 | + expect(result.testResults.map(r => r.title)).toEqual([ |
| 51 | + 'test one', |
| 52 | + 'test two', |
| 53 | + ]); |
| 54 | + expect(result.testResults[0].status).toBe('pending'); |
| 55 | + expect(result.numPendingTests).toBe(2); |
| 56 | + }); |
| 57 | + |
| 58 | + it('collects nested tests with correct ancestor titles', async () => { |
| 59 | + const root = getRunnerState().rootDescribeBlock; |
| 60 | + const outer = makeDescribe('outer', root); |
| 61 | + root.children.push(outer); |
| 62 | + const inner = makeDescribe('inner', outer); |
| 63 | + outer.children.push(inner); |
| 64 | + addTest('deep test', inner); |
| 65 | + |
| 66 | + const result = await collect(); |
| 67 | + |
| 68 | + expect(result.testResults[0].ancestorTitles).toEqual(['outer', 'inner']); |
| 69 | + expect(result.testResults[0].fullName).toBe('outer inner deep test'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('preserves source order across describe blocks', async () => { |
| 73 | + const root = getRunnerState().rootDescribeBlock; |
| 74 | + const a = makeDescribe('A', root); |
| 75 | + root.children.push(a); |
| 76 | + addTest('first', a); |
| 77 | + const b = makeDescribe('B', root); |
| 78 | + root.children.push(b); |
| 79 | + addTest('second', b); |
| 80 | + |
| 81 | + const result = await collect(); |
| 82 | + |
| 83 | + expect(result.testResults.map(r => r.title)).toEqual(['first', 'second']); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns empty results when no tests exist', async () => { |
| 87 | + const result = await collect(); |
| 88 | + expect(result.testResults).toHaveLength(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it('filters tests by testNamePattern from runner state', async () => { |
| 92 | + const state = getRunnerState(); |
| 93 | + state.testNamePattern = /one/; |
| 94 | + addTest('test one', state.rootDescribeBlock); |
| 95 | + addTest('test two', state.rootDescribeBlock); |
| 96 | + |
| 97 | + const result = await collect(); |
| 98 | + |
| 99 | + expect(result.testResults.map(r => r.title)).toEqual(['test one']); |
| 100 | + }); |
| 101 | +}); |
0 commit comments