Skip to content

Commit

Permalink
chore: typecheck example and test files (#13353)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrazauskas committed Oct 1, 2022
1 parent eafabf9 commit 8759d63
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 44 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ jobs:
run: yarn test-ts --selectProjects type-tests
- name: verify TypeScript@4.3 compatibility
run: yarn verify-old-ts
- name: typecheck examples
run: yarn typecheck:examples
- name: typecheck tests
run: yarn typecheck:tests

lint:
name: Lint
Expand Down
10 changes: 5 additions & 5 deletions examples/angular/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {describe, expect, it, jest} from '@jest/globals';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
import {AppComponent} from './app.component';
import {DataService} from './shared/data.service';

Expand All @@ -15,14 +15,14 @@ describe('AppComponent', () => {
let fixture: ComponentFixture<AppComponent>;
let app: AppComponent;

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [AppComponent],
providers: [{provide: DataService, useClass: dataServiceSpy}],
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
app = fixture.debugElement.componentInstance;
}));
});

it('should create the app', () => {
expect(app).toBeTruthy();
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
"test-ts": "yarn jest --config jest.config.ts.mjs",
"test-types": "yarn test-ts --selectProjects type-tests",
"test": "yarn lint && yarn jest",
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
"typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit",
"typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences}/src/__tests__",
"verify-old-ts": "node ./scripts/verifyOldTs.mjs",
"verify-pnp": "node ./scripts/verifyPnP.mjs",
"watch": "yarn build:js && node ./scripts/watch.mjs",
Expand Down
29 changes: 15 additions & 14 deletions packages/babel-jest/src/__tests__/getCacheKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import type {TransformOptions as BabelTransformOptions} from '@babel/core';
import type {TransformOptions as JestTransformOptions} from '@jest/transform';
import type {TransformOptions} from '@jest/transform';
import babelJest from '../index';

const {getCacheKey} = babelJest.createTransformer();
Expand Down Expand Up @@ -39,9 +39,9 @@ describe('getCacheKey', () => {
config: {rootDir: 'mock-root-dir'},
configString: 'mock-config-string',
instrument: true,
} as JestTransformOptions;
} as TransformOptions<BabelTransformOptions>;

const oldCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

test('returns cache key hash', () => {
expect(oldCacheKey.length).toEqual(32);
Expand All @@ -54,7 +54,7 @@ describe('getCacheKey', () => {

const {createTransformer}: typeof import('../index') = require('../index');

const newCacheKey = createTransformer().getCacheKey(
const newCacheKey = createTransformer().getCacheKey!(
sourceText,
sourcePath,
transformOptions,
Expand All @@ -77,7 +77,7 @@ describe('getCacheKey', () => {

const {createTransformer}: typeof import('../index') = require('../index');

const newCacheKey = createTransformer().getCacheKey(
const newCacheKey = createTransformer().getCacheKey!(
sourceText,
sourcePath,
transformOptions,
Expand All @@ -87,7 +87,7 @@ describe('getCacheKey', () => {
});

test('if `sourceText` value is changing', () => {
const newCacheKey = getCacheKey(
const newCacheKey = getCacheKey!(
'new source text',
sourcePath,
transformOptions,
Expand All @@ -97,7 +97,7 @@ describe('getCacheKey', () => {
});

test('if `sourcePath` value is changing', () => {
const newCacheKey = getCacheKey(
const newCacheKey = getCacheKey!(
sourceText,
'new-source-path.js',
transformOptions,
Expand All @@ -107,7 +107,7 @@ describe('getCacheKey', () => {
});

test('if `configString` value is changing', () => {
const newCacheKey = getCacheKey(sourceText, sourcePath, {
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
...transformOptions,
configString: 'new-config-string',
});
Expand All @@ -129,7 +129,7 @@ describe('getCacheKey', () => {

const {createTransformer}: typeof import('../index') = require('../index');

const newCacheKey = createTransformer().getCacheKey(
const newCacheKey = createTransformer().getCacheKey!(
sourceText,
sourcePath,
transformOptions,
Expand All @@ -152,7 +152,7 @@ describe('getCacheKey', () => {

const {createTransformer}: typeof import('../index') = require('../index');

const newCacheKey = createTransformer().getCacheKey(
const newCacheKey = createTransformer().getCacheKey!(
sourceText,
sourcePath,
transformOptions,
Expand All @@ -162,7 +162,7 @@ describe('getCacheKey', () => {
});

test('if `instrument` value is changing', () => {
const newCacheKey = getCacheKey(sourceText, sourcePath, {
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
...transformOptions,
instrument: false,
});
Expand All @@ -173,24 +173,25 @@ describe('getCacheKey', () => {
test('if `process.env.NODE_ENV` value is changing', () => {
process.env.NODE_ENV = 'NEW_NODE_ENV';

const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if `process.env.BABEL_ENV` value is changing', () => {
process.env.BABEL_ENV = 'NEW_BABEL_ENV';

const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

expect(oldCacheKey).not.toEqual(newCacheKey);
});

test('if node version is changing', () => {
// @ts-expect-error: Testing purpose
delete process.version;
process.version = 'new-node-version';

const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);

expect(oldCacheKey).not.toEqual(newCacheKey);
});
Expand Down
31 changes: 20 additions & 11 deletions packages/babel-jest/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

import type {
BabelFileResult,
TransformOptions as BabelTransformOptions,
} from '@babel/core';
import {makeProjectConfig} from '@jest/test-utils';
import type {TransformOptions} from '@jest/transform';
import babelJest, {createTransformer} from '../index';
import {loadPartialConfig} from '../loadBabelConfig';

jest.mock('../loadBabelConfig', () => {
const actual = jest.requireActual('@babel/core');
const actual =
jest.requireActual<typeof import('@babel/core')>('@babel/core');

return {
loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)),
Expand All @@ -20,7 +25,7 @@ jest.mock('../loadBabelConfig', () => {
};
});

const defaultBabelJestTransformer = babelJest.createTransformer(null);
const defaultBabelJestTransformer = babelJest.createTransformer();

//Mock data for all the tests
const sourceString = `
Expand Down Expand Up @@ -49,15 +54,18 @@ test('Returns source string with inline maps when no transformOptions is passed'
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
},
) as any;
} as TransformOptions<BabelTransformOptions>,
);

expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toMatch('//# sourceMappingURL');
expect(result.code).toMatch('customMultiply');
expect(result.map!.sources).toEqual(['dummy_path.js']);
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
expect((result as BabelFileResult).map!.sources).toEqual(['dummy_path.js']);
expect(
JSON.stringify((result as BabelFileResult).map!.sourcesContent),
).toMatch('customMultiply');
});

test('Returns source string with inline maps when no transformOptions is passed async', async () => {
Expand All @@ -70,8 +78,9 @@ test('Returns source string with inline maps when no transformOptions is passed
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
},
} as TransformOptions<BabelTransformOptions>,
);

expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
Expand Down Expand Up @@ -125,7 +134,7 @@ describe('caller option correctly merges from defaults and options', () => {
instrument: false,
transformerConfig: {},
...input,
});
} as TransformOptions<BabelTransformOptions>);

expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
Expand All @@ -142,14 +151,14 @@ describe('caller option correctly merges from defaults and options', () => {
});

test('can pass null to createTransformer', () => {
const transformer = createTransformer(null);
const transformer = createTransformer();
transformer.process(sourceString, 'dummy_path.js', {
cacheFS: new Map<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
});
} as TransformOptions<BabelTransformOptions>);

expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(
Expand Down
28 changes: 14 additions & 14 deletions packages/diff-sequences/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const assertCommonItems = (
const countDifferences = (
aLength: number,
bLength: number,
isCommon,
isCommon: (aIndex: number, bIndex: number) => boolean,
): number => {
const dMax = aLength + bLength;
const aIndexes = [-1]; // initialize for aLast + 1 in loop when d = 0
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('no common items', () => {
// default export does not call findSubsequences nor divide

describe('negative zero is equivalent to zero for length', () => {
const countItemsNegativeZero = (aLength, bLength) => {
const countItemsNegativeZero = (aLength: number, bLength: number) => {
let n = 0;
diff(
aLength,
Expand All @@ -301,21 +301,21 @@ describe('no common items', () => {
});

test('a empty and b empty', () => {
const a = [];
const b = [];
const expected = [];
const a: Array<unknown> = [];
const b: Array<unknown> = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('a empty and b non-empty', () => {
const a = [];
const a: Array<unknown> = [];
const b = [false];
const expected = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('a non-empty and b empty', () => {
const a = [false, true];
const b = [];
const expected = [];
const b: Array<unknown> = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});

Expand All @@ -327,7 +327,7 @@ describe('no common items', () => {
// last segment cannot have a prev segment
const a = [false];
const b = [true];
const expected = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('baDeltaLength 1 odd', () => {
Expand All @@ -336,7 +336,7 @@ describe('no common items', () => {
// last segment has a prev segment because unroll a half iteration
const a = [0, 1];
const b = ['0'];
const expected = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('baDeltaLength 2 even', () => {
Expand All @@ -345,7 +345,7 @@ describe('no common items', () => {
// last segment has a prev segment
const a = [0, 1, 2, 3];
const b = ['0', '1'];
const expected = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('baDeltaLength 7 odd', () => {
Expand All @@ -354,7 +354,7 @@ describe('no common items', () => {
// last segment has a prev segment
const a = ['0', '1', '2'];
const b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const expected = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
});
Expand Down Expand Up @@ -734,7 +734,7 @@ const assertCommonSubstring = (

// Return array of substrings in a longest common subsequence of strings.
const findCommonSubstrings = (a: string, b: string): Array<string> => {
const array = [];
const array: Array<string> = [];
diff(
a.length,
b.length,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"composite": false,
"emitDeclarationOnly": false,
"noEmit": true,
"skipLibCheck": true,
"types": ["@jest/test-globals"]
}
}

0 comments on commit 8759d63

Please sign in to comment.