Skip to content

Commit

Permalink
chore: type invariant as assert function (#9228)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored and thymikee committed Nov 24, 2019
1 parent 2c1addd commit 346b074
Show file tree
Hide file tree
Showing 6 changed files with 613 additions and 576 deletions.
8 changes: 4 additions & 4 deletions packages/jest-circus/src/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ const eventHandler: Circus.EventHandler = (event, state): void => {

if (type === 'beforeAll') {
invariant(describeBlock, 'always present for `*All` hooks');
addErrorToEachTestUnderDescribe(describeBlock!, error, asyncError);
addErrorToEachTestUnderDescribe(describeBlock, error, asyncError);
} else if (type === 'afterAll') {
// Attaching `afterAll` errors to each test makes execution flow
// too complicated, so we'll consider them to be global.
state.unhandledErrors.push([error, asyncError]);
} else {
invariant(test, 'always present for `*Each` hooks');
test!.errors.push([error, asyncError]);
test.errors.push([error, asyncError]);
}
break;
}
Expand Down Expand Up @@ -178,8 +178,8 @@ const eventHandler: Circus.EventHandler = (event, state): void => {
invariant(state.originalGlobalErrorHandlers);
invariant(state.parentProcess);
restoreGlobalErrorHandlers(
state.parentProcess!,
state.originalGlobalErrorHandlers!,
state.parentProcess,
state.originalGlobalErrorHandlers,
);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-circus/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const _callCircusTest = (
return Promise.resolve();
}

return callAsyncCircusFn(test.fn!, testContext, {isHook: false, timeout})
return callAsyncCircusFn(test.fn, testContext, {isHook: false, timeout})
.then(() => dispatch({name: 'test_fn_success', test}))
.catch(error => dispatch({error, name: 'test_fn_failure', test}));
};
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-circus/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,11 @@ export const addErrorToEachTestUnderDescribe = (
}
};

export const invariant = (condition: unknown, message?: string) => {
export function invariant(
condition: unknown,
message?: string,
): asserts condition {
if (!condition) {
throw new Error(message);
}
};
}
2 changes: 0 additions & 2 deletions packages/jest-haste-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"anymatch": "^3.0.3",
"fb-watchman": "^2.0.0",
"graceful-fs": "^4.2.3",
"invariant": "^2.2.4",
"jest-serializer": "^24.9.0",
"jest-util": "^24.9.0",
"jest-worker": "^24.9.0",
Expand All @@ -26,7 +25,6 @@
"@types/anymatch": "^1.3.1",
"@types/fb-watchman": "^2.0.0",
"@types/graceful-fs": "^4.1.2",
"@types/invariant": "^2.2.29",
"@types/micromatch": "^3.1.0",
"@types/sane": "^2.0.0"
},
Expand Down
11 changes: 8 additions & 3 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as fs from 'fs';
import {tmpdir} from 'os';
import * as path from 'path';
import {NodeWatcher, Watcher as SaneWatcher} from 'sane';
import invariant = require('invariant');
import {Config} from '@jest/types';
import serializer from 'jest-serializer';
import Worker from 'jest-worker';
Expand Down Expand Up @@ -153,6 +152,12 @@ const getWhiteList = (list: Array<string> | undefined): RegExp | null => {
return null;
};

function invariant(condition: unknown, message?: string): asserts condition {
if (!condition) {
throw new Error(message);
}
}

/**
* HasteMap is a JavaScript implementation of Facebook's haste module system.
*
Expand Down Expand Up @@ -950,8 +955,8 @@ class HasteMap extends EventEmitter {
);
const fileMetadata: FileMetaData = [
'',
stat ? stat.mtime.getTime() : -1,
stat ? stat.size : 0,
stat.mtime.getTime(),
stat.size,
0,
'',
null,
Expand Down
Loading

0 comments on commit 346b074

Please sign in to comment.