Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align circus with jasmine's top-to-bottom execution order #9965

Merged
merged 4 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions e2e/__tests__/__snapshots__/globals.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ Time: <<REPLACED>>
Ran all test suites.
`;

exports[`interleaved describe and test children order 1`] = `
PASS __tests__/interleaved.test.js
✓ above
✓ below
describe
✓ inside
`;

exports[`interleaved describe and test children order 2`] = `
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: <<REPLACED>>
Ran all test suites.
`;

exports[`only 1`] = `
PASS __tests__/onlyConstructs.test.js
✓ test.only
Expand Down
52 changes: 45 additions & 7 deletions e2e/__tests__/globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,49 @@ test('basic test constructs', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR);

const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
});

test('interleaved describe and test children order', () => {
const filename = 'interleaved.test.js';
const content = `
let lastTest;
test('above', () => {
try {
expect(lastTest).toBe(undefined);
} finally {
lastTest = 'above';
}
});
describe('describe', () => {
test('inside', () => {
try {
expect(lastTest).toBe('above');
} finally {
lastTest = 'inside';
}
});
});
test('below', () => {
try {
expect(lastTest).toBe('inside');
} finally {
lastTest = 'below';
}
});
`;

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR);

const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
});

test('skips', () => {
Expand Down Expand Up @@ -106,11 +144,11 @@ test('only', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR);
expect(exitCode).toBe(0);

const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicer to see the snapshot diff rather than just the wrong number in case something goes wrong, so I changed this for all test cases in here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, good call. I've done that before as well for the same reason

});

test('cannot have describe with no implementation', () => {
Expand All @@ -121,13 +159,13 @@ test('cannot have describe with no implementation', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR);
expect(exitCode).toBe(1);

const rest = cleanStderr(stderr);
const {summary} = extractSummary(stderr);

expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(1);
});

test('cannot test with no implementation', () => {
Expand All @@ -140,11 +178,11 @@ test('cannot test with no implementation', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR);
expect(exitCode).toBe(1);

const {summary} = extractSummary(stderr);
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(1);
});

test('skips with expand arg', () => {
Expand All @@ -171,11 +209,11 @@ test('skips with expand arg', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR, ['--expand']);
expect(exitCode).toBe(0);

const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
});

test('only with expand arg', () => {
Expand All @@ -201,11 +239,11 @@ test('only with expand arg', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR, ['--expand']);
expect(exitCode).toBe(0);

const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
});

test('cannot test with no implementation with expand arg', () => {
Expand All @@ -218,11 +256,11 @@ test('cannot test with no implementation with expand arg', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR, ['--expand']);
expect(exitCode).toBe(1);

const {summary} = extractSummary(stderr);
expect(wrap(cleanStderr(stderr))).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(1);
});

test('function as descriptor', () => {
Expand All @@ -236,9 +274,9 @@ test('function as descriptor', () => {

writeFiles(TEST_DIR, {[filename]: content});
const {stderr, exitCode} = runJest(DIR);
expect(exitCode).toBe(0);

const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
});
26 changes: 14 additions & 12 deletions packages/jest-circus/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,26 @@ const eventHandler: Circus.EventHandler = (
});
}

// inherit mode from its parent describe but
// do not inherit "only" mode when there is already tests with "only" mode
const shouldInheritMode = !(
// pass mode of currentDescribeBlock to tests
// but do not when there is already a single test with "only" mode
const shouldPassMode = !(
currentDescribeBlock.mode === 'only' &&
currentDescribeBlock.tests.find(test => test.mode === 'only')
currentDescribeBlock.children.find(
SimenB marked this conversation as resolved.
Show resolved Hide resolved
child => child.type === 'test' && child.mode === 'only',
)
);

if (shouldInheritMode) {
currentDescribeBlock.tests.forEach(test => {
if (!test.mode) {
test.mode = currentDescribeBlock.mode;
if (shouldPassMode) {
currentDescribeBlock.children.forEach(child => {
if (child.type === 'test' && !child.mode) {
child.mode = currentDescribeBlock.mode;
}
});
}

if (
!state.hasFocusedTests &&
currentDescribeBlock.tests.some(test => test.mode === 'only')
currentDescribeBlock.children.some(
child => child.type === 'test' && child.mode === 'only',
)
) {
state.hasFocusedTests = true;
}
Expand Down Expand Up @@ -129,7 +131,7 @@ const eventHandler: Circus.EventHandler = (
if (test.mode === 'only') {
state.hasFocusedTests = true;
}
currentDescribeBlock.tests.push(test);
currentDescribeBlock.children.push(test);
break;
}
case 'hook_failure': {
Expand Down
33 changes: 19 additions & 14 deletions packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,25 @@ const _runTestsForDescribeBlock = async (
const retryTimes = parseInt(global[RETRY_TIMES], 10) || 0;
const deferredRetryTests = [];

for (const test of describeBlock.tests) {
const hasErrorsBeforeTestRun = test.errors.length > 0;
await _runTest(test);

if (
hasErrorsBeforeTestRun === false &&
retryTimes > 0 &&
test.errors.length > 0
) {
deferredRetryTests.push(test);
for (const child of describeBlock.children) {
switch (child.type) {
case 'describeBlock': {
await _runTestsForDescribeBlock(child);
break;
}
case 'test': {
const hasErrorsBeforeTestRun = child.errors.length > 0;
await _runTest(child);

if (
hasErrorsBeforeTestRun === false &&
retryTimes > 0 &&
child.errors.length > 0
) {
deferredRetryTests.push(child);
}
break;
}
}
}

Expand All @@ -69,10 +78,6 @@ const _runTestsForDescribeBlock = async (
}
}

for (const child of describeBlock.children) {
await _runTestsForDescribeBlock(child);
}

for (const hook of afterAll) {
await _callCircusHook({describeBlock, hook});
}
Expand Down
Loading