Skip to content

Commit 6d8185b

Browse files
authored
chore: use async-await in tests (#10970)
1 parent b51564b commit 6d8185b

19 files changed

+1984
-2044
lines changed

packages/jest-haste-map/src/__tests__/index.test.js

Lines changed: 487 additions & 529 deletions
Large diffs are not rendered by default.

packages/jest-haste-map/src/crawlers/__tests__/node.test.js

Lines changed: 134 additions & 135 deletions
Large diffs are not rendered by default.

packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js

Lines changed: 159 additions & 159 deletions
Large diffs are not rendered by default.

packages/jest-haste-map/src/watchers/FSEventsWatcher.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,12 @@ class FSEventsWatcher extends EventEmitter {
130130
/**
131131
* End watching.
132132
*/
133-
close(callback?: () => void): void {
134-
this.fsEventsWatchStopper().then(() => {
135-
this.removeAllListeners();
136-
if (typeof callback === 'function') {
137-
process.nextTick(callback.bind(null, null, true));
138-
}
139-
});
133+
async close(callback?: () => void): Promise<void> {
134+
await this.fsEventsWatchStopper();
135+
this.removeAllListeners();
136+
if (typeof callback === 'function') {
137+
process.nextTick(callback.bind(null, null, true));
138+
}
140139
}
141140

142141
private isFileIncluded(relativePath: string) {

packages/jest-runtime/src/__tests__/Runtime-sourceMaps.test.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,28 @@ describe('Runtime', () => {
1515
});
1616

1717
describe('requireModule', () => {
18-
it('installs source maps if available', () => {
18+
it('installs source maps if available', async () => {
1919
expect.assertions(1);
2020

21-
return createRuntime(__filename).then(runtime => {
22-
const sum = runtime.requireModule(
23-
runtime.__mockRootPath,
24-
'./sourcemaps/out/throwing-mapped-fn.js',
25-
).sum;
21+
const runtime = await createRuntime(__filename);
22+
const sum = runtime.requireModule(
23+
runtime.__mockRootPath,
24+
'./sourcemaps/out/throwing-mapped-fn.js',
25+
).sum;
2626

27-
try {
28-
sum();
29-
} catch (err) {
30-
if (process.platform === 'win32') {
31-
expect(err.stack).toMatch(
32-
/^Error: throwing fn\s+at sum.+\\__tests__\\test_root\\sourcemaps\\(out\\)?throwing-mapped-fn.js:\d+:\d+/,
33-
);
34-
} else {
35-
expect(err.stack).toMatch(
36-
/^Error: throwing fn\s+at sum.+\/__tests__\/test_root\/sourcemaps\/(out\/)?throwing-mapped-fn.js:\d+:\d+/,
37-
);
38-
}
27+
try {
28+
sum();
29+
} catch (err) {
30+
if (process.platform === 'win32') {
31+
expect(err.stack).toMatch(
32+
/^Error: throwing fn\s+at sum.+\\__tests__\\test_root\\sourcemaps\\(out\\)?throwing-mapped-fn.js:\d+:\d+/,
33+
);
34+
} else {
35+
expect(err.stack).toMatch(
36+
/^Error: throwing fn\s+at sum.+\/__tests__\/test_root\/sourcemaps\/(out\/)?throwing-mapped-fn.js:\d+:\d+/,
37+
);
3938
}
40-
});
39+
}
4140
});
4241
});
4342
});

packages/jest-runtime/src/__tests__/runtime_create_mock_from_module.test.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,48 @@ describe('Runtime', () => {
2020
});
2121

2222
describe('createMockFromModule', () => {
23-
it('does not cause side effects in the rest of the module system when generating a mock', () =>
24-
createRuntime(__filename).then(runtime => {
25-
const testRequire = runtime.requireModule.bind(
26-
runtime,
27-
runtime.__mockRootPath,
28-
);
23+
it('does not cause side effects in the rest of the module system when generating a mock', async () => {
24+
const runtime = await createRuntime(__filename);
25+
const testRequire = runtime.requireModule.bind(
26+
runtime,
27+
runtime.__mockRootPath,
28+
);
2929

30-
const module = testRequire('RegularModule');
31-
const origModuleStateValue = module.getModuleStateValue();
30+
const module = testRequire('RegularModule');
31+
const origModuleStateValue = module.getModuleStateValue();
3232

33-
expect(origModuleStateValue).toBe('default');
33+
expect(origModuleStateValue).toBe('default');
3434

35-
// Generate a mock for a module with side effects
36-
const mock = module.jest.createMockFromModule('ModuleWithSideEffects');
35+
// Generate a mock for a module with side effects
36+
const mock = module.jest.createMockFromModule('ModuleWithSideEffects');
3737

38-
// Make sure we get a mock.
39-
expect(mock.fn()).toBe(undefined);
40-
expect(module.getModuleStateValue()).toBe(origModuleStateValue);
41-
}));
38+
// Make sure we get a mock.
39+
expect(mock.fn()).toBe(undefined);
40+
expect(module.getModuleStateValue()).toBe(origModuleStateValue);
41+
});
4242

43-
it('resolves mapped modules correctly', () =>
44-
createRuntime(__filename, {moduleNameMapper}).then(runtime => {
45-
const root = runtime.requireModule(runtime.__mockRootPath);
46-
const mockModule = root.jest.createMockFromModule(
47-
'module/name/createMockFromModule',
48-
);
43+
it('resolves mapped modules correctly', async () => {
44+
const runtime = await createRuntime(__filename, {moduleNameMapper});
45+
const root = runtime.requireModule(runtime.__mockRootPath);
46+
const mockModule = root.jest.createMockFromModule(
47+
'module/name/createMockFromModule',
48+
);
4949

50-
expect(mockModule.test.mock).toBeTruthy();
51-
}));
50+
expect(mockModule.test.mock).toBeTruthy();
51+
});
5252
});
5353

54-
it('creates mock objects in the right environment', () =>
55-
createRuntime(__filename).then(runtime => {
56-
const testRequire = runtime.requireModule.bind(
57-
runtime,
58-
runtime.__mockRootPath,
59-
);
54+
it('creates mock objects in the right environment', async () => {
55+
const runtime = await createRuntime(__filename);
56+
const testRequire = runtime.requireModule.bind(
57+
runtime,
58+
runtime.__mockRootPath,
59+
);
6060

61-
const module = testRequire('RegularModule');
62-
const mockModule = module.jest.createMockFromModule('RegularModule');
63-
const testObjectPrototype = Object.getPrototypeOf(module.object);
64-
const mockObjectPrototype = Object.getPrototypeOf(mockModule.object);
65-
expect(mockObjectPrototype).toBe(testObjectPrototype);
66-
}));
61+
const module = testRequire('RegularModule');
62+
const mockModule = module.jest.createMockFromModule('RegularModule');
63+
const testObjectPrototype = Object.getPrototypeOf(module.object);
64+
const mockObjectPrototype = Object.getPrototypeOf(mockModule.object);
65+
expect(mockObjectPrototype).toBe(testObjectPrototype);
66+
});
6767
});

packages/jest-runtime/src/__tests__/runtime_environment.test.js

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,41 @@ describe('Runtime', () => {
1616
});
1717

1818
describe('requireModule', () => {
19-
it('emulates a node stack trace during module load', () =>
20-
createRuntime(__filename).then(runtime => {
21-
let hasThrown = false;
22-
try {
23-
runtime.requireModule(runtime.__mockRootPath, './throwing.js');
24-
} catch (err) {
25-
hasThrown = true;
26-
expect(err.stack).toMatch(/^Error: throwing\s+at Object.<anonymous>/);
27-
}
28-
expect(hasThrown).toBe(true);
29-
}));
19+
it('emulates a node stack trace during module load', async () => {
20+
const runtime = await createRuntime(__filename);
21+
let hasThrown = false;
22+
try {
23+
runtime.requireModule(runtime.__mockRootPath, './throwing.js');
24+
} catch (err) {
25+
hasThrown = true;
26+
expect(err.stack).toMatch(/^Error: throwing\s+at Object.<anonymous>/);
27+
}
28+
expect(hasThrown).toBe(true);
29+
});
3030

31-
it('emulates a node stack trace during function execution', () =>
32-
createRuntime(__filename).then(runtime => {
33-
let hasThrown = false;
34-
const sum = runtime.requireModule(
35-
runtime.__mockRootPath,
36-
'./throwing_fn.js',
37-
);
31+
it('emulates a node stack trace during function execution', async () => {
32+
const runtime = await createRuntime(__filename);
33+
let hasThrown = false;
34+
const sum = runtime.requireModule(
35+
runtime.__mockRootPath,
36+
'./throwing_fn.js',
37+
);
3838

39-
try {
40-
sum();
41-
} catch (err) {
42-
hasThrown = true;
43-
if (process.platform === 'win32') {
44-
expect(err.stack).toMatch(
45-
/^Error: throwing fn\s+at sum.+\\__tests__\\test_root\\throwing_fn\.js/,
46-
);
47-
} else {
48-
expect(err.stack).toMatch(
49-
/^Error: throwing fn\s+at sum.+\/__tests__\/test_root\/throwing_fn\.js/,
50-
);
51-
}
39+
try {
40+
sum();
41+
} catch (err) {
42+
hasThrown = true;
43+
if (process.platform === 'win32') {
44+
expect(err.stack).toMatch(
45+
/^Error: throwing fn\s+at sum.+\\__tests__\\test_root\\throwing_fn\.js/,
46+
);
47+
} else {
48+
expect(err.stack).toMatch(
49+
/^Error: throwing fn\s+at sum.+\/__tests__\/test_root\/throwing_fn\.js/,
50+
);
5251
}
53-
expect(hasThrown).toBe(true);
54-
}));
52+
}
53+
expect(hasThrown).toBe(true);
54+
});
5555
});
5656
});

packages/jest-runtime/src/__tests__/runtime_internal_module.test.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,53 @@ describe('Runtime', () => {
2020
});
2121

2222
describe('internalModule', () => {
23-
it('loads modules and applies transforms', () =>
24-
createRuntime(__filename, {
23+
it('loads modules and applies transforms', async () => {
24+
const runtime = await createRuntime(__filename, {
2525
transform: {'\\.js$': './test_preprocessor'},
26-
}).then(runtime => {
27-
const modulePath = path.resolve(
28-
path.dirname(runtime.__mockRootPath),
29-
'internal-root.js',
30-
);
31-
expect(() => {
32-
runtime.requireModule(modulePath);
33-
}).toThrow(new Error('preprocessor must not run.'));
34-
}));
26+
});
27+
const modulePath = path.resolve(
28+
path.dirname(runtime.__mockRootPath),
29+
'internal-root.js',
30+
);
31+
expect(() => {
32+
runtime.requireModule(modulePath);
33+
}).toThrow(new Error('preprocessor must not run.'));
34+
});
3535

36-
it('loads internal modules without applying transforms', () =>
37-
createRuntime(__filename, {
36+
it('loads internal modules without applying transforms', async () => {
37+
const runtime = await createRuntime(__filename, {
3838
transform: {'\\.js$': './test_preprocessor'},
39-
}).then(runtime => {
40-
const modulePath = path.resolve(
41-
path.dirname(runtime.__mockRootPath),
42-
'internal-root.js',
43-
);
44-
const exports = runtime.requireInternalModule(modulePath);
45-
expect(exports()).toBe('internal-module-data');
46-
}));
39+
});
40+
const modulePath = path.resolve(
41+
path.dirname(runtime.__mockRootPath),
42+
'internal-root.js',
43+
);
44+
const exports = runtime.requireInternalModule(modulePath);
45+
expect(exports()).toBe('internal-module-data');
46+
});
4747

48-
it('loads JSON modules and applies transforms', () =>
49-
createRuntime(__filename, {
48+
it('loads JSON modules and applies transforms', async () => {
49+
const runtime = await createRuntime(__filename, {
5050
transform: {'\\.json$': './test_json_preprocessor'},
51-
}).then(runtime => {
52-
const modulePath = path.resolve(
53-
path.dirname(runtime.__mockRootPath),
54-
'internal-root.json',
55-
);
56-
const exports = runtime.requireModule(modulePath);
57-
expect(exports).toEqual({foo: 'foo'});
58-
}));
51+
});
52+
const modulePath = path.resolve(
53+
path.dirname(runtime.__mockRootPath),
54+
'internal-root.json',
55+
);
56+
const exports = runtime.requireModule(modulePath);
57+
expect(exports).toEqual({foo: 'foo'});
58+
});
5959

60-
it('loads internal JSON modules without applying transforms', () =>
61-
createRuntime(__filename, {
60+
it('loads internal JSON modules without applying transforms', async () => {
61+
const runtime = await createRuntime(__filename, {
6262
transform: {'\\.json$': './test_json_preprocessor'},
63-
}).then(runtime => {
64-
const modulePath = path.resolve(
65-
path.dirname(runtime.__mockRootPath),
66-
'internal-root.json',
67-
);
68-
const exports = runtime.requireInternalModule(modulePath);
69-
expect(exports).toEqual({foo: 'bar'});
70-
}));
63+
});
64+
const modulePath = path.resolve(
65+
path.dirname(runtime.__mockRootPath),
66+
'internal-root.json',
67+
);
68+
const exports = runtime.requireInternalModule(modulePath);
69+
expect(exports).toEqual({foo: 'bar'});
70+
});
7171
});
7272
});

0 commit comments

Comments
 (0)