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

feat: remove testURL option, can be set in testEnvironmentOptions #10797

Merged
merged 18 commits into from Feb 23, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- `[babel-jest]` Export `createTransformer` function ([#12399](https://github.com/facebook/jest/pull/12399))
- `[expect]` Expose `AsymmetricMatchers`, `MatcherFunction` and `MatcherFunctionWithState` interfaces ([#12363](https://github.com/facebook/jest/pull/12363), [#12376](https://github.com/facebook/jest/pull/12376))
- `[jest-cli, jest-config]` [**BREAKING**] Remove `testURL` config, use `testEnvironmentOptions.url` instead ([#10797](https://github.com/facebook/jest/pull/10797))
- `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354))
- `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355))
- `[jest-config, @jest/types]` Add `ci` to `GlobalConfig` ([#12378](https://github.com/facebook/jest/pull/12378))
Expand Down
8 changes: 1 addition & 7 deletions docs/Configuration.md
Expand Up @@ -1168,7 +1168,7 @@ beforeAll(() => {

Default: `{}`

Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [jsdom](https://github.com/jsdom/jsdom) such as `{html: "<html lang="zh-cmn-Hant"></html>", userAgent: "Agent/007"}`.
Test environment options that will be passed to the `testEnvironment`. The relevant options depend on the environment. For example, you can override options given to [`jsdom`](https://github.com/jsdom/jsdom) such as `{html: "<html lang="zh-cmn-Hant"></html>", url: 'https://jestjs.io/', userAgent: "Agent/007"}`.

### `testFailureExitCode` \[number]

Expand Down Expand Up @@ -1332,12 +1332,6 @@ Default: `5000`

Default timeout of a test in milliseconds.

### `testURL` \[string]

Default: `http://localhost`

This option sets the URL for the jsdom environment. It is reflected in properties such as `location.href`.

### `timers` \[string]

Default: `real`
Expand Down
1 change: 0 additions & 1 deletion e2e/__tests__/__snapshots__/showConfig.test.ts.snap
Expand Up @@ -66,7 +66,6 @@ exports[`--showConfig outputs config info and exits 1`] = `
],
"testRegex": [],
"testRunner": "<<REPLACED_JEST_PACKAGES_DIR>>/jest-circus/runner.js",
"testURL": "http://localhost",
"timers": "real",
"transform": [
[
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-cli/src/cli/args.ts
Expand Up @@ -616,10 +616,6 @@ export const options = {
description: 'This option sets the default timeouts of test cases.',
type: 'number',
},
testURL: {
description: 'This option sets the URL for the jsdom environment.',
type: 'string',
},
timers: {
description:
'Setting this value to fake allows the use of fake timers ' +
Expand Down
Expand Up @@ -283,9 +283,6 @@ module.exports = {
// This option allows use of a custom test runner
// testRunner: "jest-circus/runner",

// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",

// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",

Expand Down
1 change: 0 additions & 1 deletion packages/jest-config/src/Defaults.ts
Expand Up @@ -72,7 +72,6 @@ const defaultOptions: Config.DefaultOptions = {
testRegex: [],
testRunner: 'jest-circus/runner',
testSequencer: '@jest/test-sequencer',
testURL: 'http://localhost',
timers: 'real',
transformIgnorePatterns: [NODE_MODULES_REGEXP, `\\.pnp\\.[^\\${sep}]+$`],
useStderr: false,
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-config/src/Deprecated.ts
Expand Up @@ -74,6 +74,14 @@ const deprecatedOptions: DeprecatedOptions = {

Please update your configuration.
`,

testURL: (_options: {testURL?: string}) => ` Option ${chalk.bold(
'"testURL"',
)} was replaced by passing the URL via ${chalk.bold(
'"testEnvironmentOptions.url"',
)}.

Please update your configuration.`,
};

export default deprecatedOptions;
2 changes: 0 additions & 2 deletions packages/jest-config/src/Descriptions.ts
Expand Up @@ -84,8 +84,6 @@ const descriptions: {[key in keyof Config.InitialOptions]: string} = {
testResultsProcessor:
'This option allows the use of a custom results processor',
testRunner: 'This option allows use of a custom test runner',
testURL:
'This option sets the URL for the jsdom environment. It is reflected in properties such as location.href',
timers:
'Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"',
transform: 'A map from regular expressions to paths to transformers',
Expand Down
6 changes: 4 additions & 2 deletions packages/jest-config/src/ValidConfig.ts
Expand Up @@ -115,7 +115,10 @@ const initialOptions: Config.InitialOptions = {
snapshotResolver: '<rootDir>/snapshotResolver.js',
snapshotSerializers: ['my-serializer-module'],
testEnvironment: 'jest-environment-node',
testEnvironmentOptions: {userAgent: 'Agent/007'},
testEnvironmentOptions: {
url: 'http://localhost',
userAgent: 'Agent/007',
},
testFailureExitCode: 1,
testLocationInResults: false,
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
Expand All @@ -129,7 +132,6 @@ const initialOptions: Config.InitialOptions = {
testRunner: 'circus',
testSequencer: '@jest/test-sequencer',
testTimeout: 5000,
testURL: 'http://localhost',
timers: 'real',
transform: {
'\\.js$': '<rootDir>/preprocessor.js',
Expand Down
Expand Up @@ -229,6 +229,30 @@ exports[`testTimeout should throw an error if timeout is a negative number 1`] =
<red></>"
`;

exports[`testURL logs a deprecation warning when \`testURL\` is used 1`] = `
[MockFunction] {
"calls": Array [
Array [
"<yellow><bold><bold>●</><bold> Deprecation Warning</>:</>
<yellow></>
<yellow> Option <bold>"testURL"</> was replaced by passing the URL via <bold>"testEnvironmentOptions.url"</>.</>
<yellow></>
<yellow> Please update your configuration.</>
<yellow></>
<yellow> <bold>Configuration Documentation:</></>
<yellow> https://jestjs.io/docs/configuration</>
<yellow></>",
],
],
"results": Array [
Object {
"type": "return",
"value": undefined,
},
],
}
`;

exports[`watchPlugins throw error when a watch plugin is not found 1`] = `
"<red><bold><bold>● </><bold>Validation Error</>:</>
<red></>
Expand Down
18 changes: 18 additions & 0 deletions packages/jest-config/src/__tests__/normalize.test.ts
Expand Up @@ -1918,3 +1918,21 @@ describe('updateSnapshot', () => {
Defaults.ci = defaultCiConfig;
});
});

describe('testURL', () => {
beforeEach(() => {
jest.mocked(console.warn).mockImplementation(() => {});
});

it('logs a deprecation warning when `testURL` is used', async () => {
await normalize(
{
rootDir: '/root/',
testURL: 'https://jestjs.io/',
},
{} as Config.Argv,
);

expect(console.warn).toMatchSnapshot();
});
});
1 change: 0 additions & 1 deletion packages/jest-config/src/index.ts
Expand Up @@ -222,7 +222,6 @@ const groupOptions = (
testPathIgnorePatterns: options.testPathIgnorePatterns,
testRegex: options.testRegex,
testRunner: options.testRunner,
testURL: options.testURL,
timers: options.timers,
transform: options.transform,
transformIgnorePatterns: options.transformIgnorePatterns,
Expand Down
1 change: 0 additions & 1 deletion packages/jest-config/src/normalize.ts
Expand Up @@ -1014,7 +1014,6 @@ export default async function normalize(
case 'testFailureExitCode':
case 'testLocationInResults':
case 'testNamePattern':
case 'testURL':
case 'timers':
case 'useStderr':
case 'verbose':
Expand Down
Expand Up @@ -52,7 +52,6 @@ exports[`prints the config object 1`] = `
"\\\\.test\\\\.js$"
],
"testRunner": "myRunner",
"testURL": "http://localhost",
"timers": "real",
"transform": [],
"transformIgnorePatterns": [],
Expand Down
Expand Up @@ -56,6 +56,32 @@ describe('JSDomEnvironment', () => {
expect(env.dom.window.navigator.userAgent).toEqual('foo');
});

it('should respect url option', () => {
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
},
{console, docblockPragmas: {}, testPath: __filename},
);

expect(env.dom.window.location.href).toEqual('http://localhost/');

const envWithUrl = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig({
testEnvironmentOptions: {
url: 'https://jestjs.io',
},
}),
},
{console, docblockPragmas: {}, testPath: __filename},
);

expect(envWithUrl.dom.window.location.href).toEqual('https://jestjs.io/');
});

/**
* When used in conjunction with Custom Elements (part of the WebComponents standard)
* setting the global.document to null too early is problematic because:
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -49,7 +49,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
})
: undefined,
runScripts: 'dangerously',
url: projectConfig.testURL,
url: 'http://localhost/',
Copy link
Member

Choose a reason for hiding this comment

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

this keeps our current default

virtualConsole: new VirtualConsole().sendTo(context.console),
...projectConfig.testEnvironmentOptions,
},
Expand Down