forked from jestjs/jest
/
injectGlobals.test.ts
57 lines (47 loc) 路 1.63 KB
/
injectGlobals.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as path from 'path';
import {tmpdir} from 'os';
import {wrap} from 'jest-snapshot-serializer-raw';
import {skipSuiteOnJasmine} from '@jest/test-utils';
import {json as runJest} from '../runJest';
import {
cleanup,
createEmptyPackage,
extractSummary,
writeFiles,
} from '../Utils';
const DIR = path.resolve(tmpdir(), 'injectGlobalVariables.test');
const TEST_DIR = path.resolve(DIR, '__tests__');
skipSuiteOnJasmine();
beforeEach(() => {
cleanup(DIR);
createEmptyPackage(DIR);
const content = `
const {expect: importedExpect, test: importedTest} = require('@jest/globals');
importedTest('no globals injected', () =>{
importedExpect(typeof expect).toBe('undefined');
importedExpect(typeof test).toBe('undefined');
importedExpect(typeof jest).toBe('undefined');
importedExpect(typeof beforeEach).toBe('undefined');
});
`;
writeFiles(TEST_DIR, {'test.js': content});
});
afterAll(() => cleanup(DIR));
test.each`
configSource | args
${'CLI'} | ${['--inject-globals', 'false']}
${'config'} | ${['--config', JSON.stringify({injectGlobals: false})]}
`('globals are undefined if passed `false` from $configSource', ({args}) => {
const {json, stderr, exitCode} = runJest(DIR, args);
const {summary, rest} = extractSummary(stderr);
expect(wrap(rest)).toMatchSnapshot();
expect(wrap(summary)).toMatchSnapshot();
expect(exitCode).toBe(0);
expect(json.numPassedTests).toBe(1);
});