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

chore: fix some type errors in tests #13360

Merged
merged 8 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ Example:

```js
// my-custom-environment
const NodeEnvironment = require('jest-environment-node').default;
const NodeEnvironment = require('jest-environment-node').TestEnvironment;

class CustomEnvironment extends NodeEnvironment {
constructor(config, context) {
Expand Down
2 changes: 1 addition & 1 deletion docs/Puppeteer.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const {readFile} = require('fs').promises;
const os = require('os');
const path = require('path');
const puppeteer = require('puppeteer');
const NodeEnvironment = require('jest-environment-node').default;
const NodeEnvironment = require('jest-environment-node').TestEnvironment;

const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup');

Expand Down
4 changes: 2 additions & 2 deletions e2e/global-setup-esm/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import os from 'os';
import path from 'path';
import * as os from 'os';
import * as path from 'path';
import fs from 'graceful-fs';
import greeting from '../';

Expand Down
6 changes: 3 additions & 3 deletions e2e/global-setup-esm/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import crypto from 'crypto';
import os from 'os';
import path from 'path';
import * as crypto from 'crypto';
import * as os from 'os';
import * as path from 'path';
import fs from 'graceful-fs';
import jestUtil from 'jest-util';

Expand Down
4 changes: 2 additions & 2 deletions e2e/global-teardown-esm/__tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import os from 'os';
import path from 'path';
import * as os from 'os';
import * as path from 'path';
import fs from 'graceful-fs';
import greeting from '../';

Expand Down
6 changes: 3 additions & 3 deletions e2e/global-teardown-esm/teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import crypto from 'crypto';
import os from 'os';
import path from 'path';
import * as crypto from 'crypto';
import * as os from 'os';
import * as path from 'path';
import fs from 'graceful-fs';
import jestUtil from 'jest-util';

Expand Down
2 changes: 1 addition & 1 deletion e2e/require-main/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import * as path from 'path';

module.exports = function load(moduleId) {
return require(path.join(path.dirname(require.main.filename), moduleId));
Expand Down
2 changes: 1 addition & 1 deletion e2e/resolve-conditions/deno-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const NodeEnv = require('jest-environment-node').default;
const NodeEnv = require('jest-environment-node').TestEnvironment;

module.exports = class DenoEnvWithConditions extends NodeEnv {
exportConditions() {
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment-async/TestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const fs = require('fs');
const os = require('os');
const JSDOMEnvironment = require('jest-environment-jsdom').default;
const JSDOMEnvironment = require('jest-environment-jsdom').TestEnvironment;
const {createDirectory} = require('jest-util');

const DIR = `${os.tmpdir()}/jest-test-environment`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const JSDOMEnvironment = require('jest-environment-jsdom').default;
const JSDOMEnvironment = require('jest-environment-jsdom').TestEnvironment;

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const JSDOMEnvironment = require('jest-environment-jsdom').default;
const JSDOMEnvironment = require('jest-environment-jsdom').TestEnvironment;

class TestEnvironment extends JSDOMEnvironment {
handleTestEvent(event) {
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment/DocblockPragmasEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

const JSDOMEnvironment = require('jest-environment-jsdom').default;
const JSDOMEnvironment = require('jest-environment-jsdom').TestEnvironment;

class TestEnvironment extends JSDOMEnvironment {
constructor(config, context) {
Expand Down
2 changes: 1 addition & 1 deletion e2e/test-environment/EsmDefaultEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

exports.__esModule = true;

const NodeEnvironment = require('jest-environment-node').default;
const NodeEnvironment = require('jest-environment-node').TestEnvironment;

class Env extends NodeEnvironment {
constructor(config, options) {
Expand Down
9 changes: 6 additions & 3 deletions e2e/transform/transform-environment/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

import type {Config} from '@jest/types';
import type {
EnvironmentContext,
JestEnvironmentConfig,
} from '@jest/environment';
import NodeEnvironment from 'jest-environment-node';

export default class CustomEnvironment extends NodeEnvironment {
constructor(config: Config.ProjectConfig) {
super(config);
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
super(config, context);
this.global.one = 1;
}
}
12 changes: 6 additions & 6 deletions examples/mongodb/mongo-environment.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from 'fs';
import path from 'path';
import NodeEnvironment from 'jest-environment-node';
import * as fs from 'fs';
import * as path from 'path';
import {TestEnvironment} from 'jest-environment-node';

const globalConfigPath = path.join(__dirname, 'globalConfig.json');

class MongoEnvironment extends NodeEnvironment {
constructor(config) {
super(config);
class MongoEnvironment extends TestEnvironment {
constructor(config, context) {
super(config, context);
}

async setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/mongodb/setup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import * as fs from 'fs';
import * as path from 'path';
import {MongoMemoryServer} from 'mongodb-memory-server';

const globalConfigPath = path.join(__dirname, 'globalConfig.json');
Expand Down
4 changes: 2 additions & 2 deletions examples/mongodb/teardown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import path from 'path';
import * as fs from 'fs';
import * as path from 'path';

const globalConfigPath = path.join(__dirname, 'globalConfig.json');

Expand Down
18 changes: 13 additions & 5 deletions packages/expect-utils/src/__tests__/isError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@ import {isError} from '../utils';
// Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/test/AngularSpec.js#L1883
describe('isError', () => {
function testErrorFromDifferentContext(
createError: (win: Window | null) => Error,
createError: (win: Window) => Error | null,
) {
const iframe = document.createElement('iframe');
document.body.appendChild(iframe);
try {
const error = createError(iframe.contentWindow);
const contentWindow = iframe.contentWindow;

expect(contentWindow).toBeTruthy();

if (!contentWindow) {
throw new Error('Dead code');
}

const error = createError(contentWindow);
expect(isError(error)).toBe(true);
} finally {
iframe.parentElement!.removeChild(iframe);
Expand All @@ -38,14 +46,14 @@ describe('isError', () => {
});

it('should detect errors from another context', () => {
testErrorFromDifferentContext((win: Window) => new win.Error());
testErrorFromDifferentContext(win => new win.Error());
});

it('should detect DOMException errors from another context', () => {
testErrorFromDifferentContext((win: Window) => {
testErrorFromDifferentContext(win => {
try {
win.document.querySelectorAll('');
} catch (e) {
} catch (e: any) {
return e;
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/assertionCounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('.hasAssertions()', () => {
});

it('throws if expected is not undefined', () => {
jestExpect(() => {
expect(() => {
// @ts-expect-error
jestExpect.hasAssertions(2);
}).toThrowErrorMatchingSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('toEqual', () => {
});

it('should be symmetric', () => {
const safeExpectEqual = (a, b) => {
const safeExpectEqual = (a: unknown, b: unknown) => {
try {
expect(a).toEqual(b);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import assert from 'assert';
import * as assert from 'assert';
import fc from 'fast-check';
import expect from '..';
import {
Expand All @@ -15,15 +15,15 @@ import {
} from './__arbitraries__/sharedSettings';

describe('toStrictEqual', () => {
const safeExpectStrictEqual = (a, b) => {
const safeExpectStrictEqual = (a: unknown, b: unknown) => {
try {
expect(a).toStrictEqual(b);
return true;
} catch {
return false;
}
};
const safeAssertDeepStrictEqual = (a, b) => {
const safeAssertDeepStrictEqual = (a: unknown, b: unknown) => {
try {
assert.deepStrictEqual(a, b);
return true;
Expand Down
2 changes: 1 addition & 1 deletion packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('.rejects', () => {
});

it('should reject async function to toThrow', async () => {
await expect(async () => {
await jestExpect(async () => {
throw new Error('Test');
}).rejects.toThrow('Test');
});
Expand Down
Loading