Skip to content

Commit

Permalink
refactor: adopt ts-jest 26.4.4 (#665)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Dec 12, 2020
1 parent e2a1353 commit 12189f1
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 118 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1"
},
"dependencies": {
"@jest/create-cache-key-function": "^26.5.0",
"jest-environment-jsdom": "^26.6.2",
"pretty-format": "26.x",
"ts-jest": "^26.4.4"
Expand Down
62 changes: 48 additions & 14 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
describe('AngularJestTransformer', () => {
import { TsJestTransformer } from 'ts-jest/dist/ts-jest-transformer';

describe('NgJestTransformer', () => {
describe('getCacheKey', () => {
it('should be different for each argument value', () => {
// eslint-disable-next-line
const tr = require('../');
test('should call getCacheKey method from parent class TsJestTransformer', () => {
TsJestTransformer.prototype.getCacheKey = jest.fn();
const input = {
fileContent: 'export default "foo"',
fileName: 'foo.ts',
jestConfigStr: '{"foo": "bar"}',
// eslint-disable-next-line
options: { config: { foo: 'bar' } as any, instrument: false, rootDir: '/foo' },
};
const keys = [
tr.getCacheKey(input.fileContent, input.fileName, input.jestConfigStr, input.options),
tr.getCacheKey(input.fileContent, 'bar.ts', input.jestConfigStr, input.options),
tr.getCacheKey(input.fileContent, input.fileName, '{}', { ...input.options, instrument: true }),
tr.getCacheKey(input.fileContent, input.fileName, '{}', { ...input.options, rootDir: '/bar' }),
];
// each key should have correct length
for (const key of keys) {
expect(key).toHaveLength(32);
}
// eslint-disable-next-line
const tr = require('../');

tr.getCacheKey(input.fileContent, input.fileName, input.jestConfigStr, input.options);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(TsJestTransformer.prototype.getCacheKey).toHaveBeenCalledWith(
input.fileContent,
input.fileName,
input.jestConfigStr,
input.options,
);
});
});

describe('configsFor', () => {
test(
'should return the same config set for same values with different jest config objects' +
' but their serialized versions are the same',
() => {
const obj1 = {
config: { cwd: '/foo/.', rootDir: '/bar//dummy/..', globals: {}, testMatch: [], testRegex: [] },
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const obj2 = { ...obj1, config: { ...obj1.config, globals: Object.create(null) } };
// eslint-disable-next-line
const cs1 = require('../').configsFor(obj1);
// eslint-disable-next-line
const cs2 = require('../').configsFor(obj2);

expect(cs2).toBe(cs1);
},
);

test('should return the same config set for same values with jest config objects', () => {
const obj1 = { config: { cwd: '/foo/.', rootDir: '/bar//dummy/..', globals: {}, testMatch: [], testRegex: [] } };
const obj2 = { ...obj1 };
// eslint-disable-next-line
const cs1 = require('../').configsFor(obj1);
// eslint-disable-next-line
const cs2 = require('../').configsFor(obj2);

expect(cs2).toBe(cs1);
});
});
});
69 changes: 30 additions & 39 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import type { ParsedConfiguration } from '@angular/compiler-cli';
import type { CacheKeyOptions, TransformedSource, Transformer, TransformOptions } from '@jest/transform';
import type { TransformedSource, Transformer, TransformOptions } from '@jest/transform';
import type { Config } from '@jest/types';
import createCacheKey from '@jest/create-cache-key-function';
import { stringify } from 'ts-jest/dist/utils/json';
import { JsonableValue } from 'ts-jest/dist/utils/jsonable-value';
import { TsJestTransformer } from 'ts-jest/dist/ts-jest-transformer';

import { NgJestConfig } from './config/ng-jest-config';
import { JsonObject } from './utils/json-object';
import { stringify } from './utils/json';

interface CachedConfigSet {
ngJestConfig: NgJestConfig;
jestConfig: JsonObject<Config.ProjectConfig>;
transformerConfig: JsonObject<Config.ProjectConfig & ParsedConfiguration>;
jestConfig: JsonableValue<Config.ProjectConfig>;
transformerCfgStr: string;
}

class AngularJestTransformer extends TsJestTransformer implements Transformer {
class NgJestTransformer extends TsJestTransformer implements Transformer {
/**
* cache config set between each test run
*/
private static readonly _cachedConfigSets: CachedConfigSet[] = [];
private _transformerCfgStr!: string;
// @ts-expect-error Temporarily use ts-expect-error because we will use this later
private _ngJestConfig!: NgJestConfig;

Expand All @@ -32,56 +29,50 @@ class AngularJestTransformer extends TsJestTransformer implements Transformer {
return super.process(input, filePath, jestConfig, transformOptions);
}

getCacheKey(
fileContent: string,
filePath: string,
_jestConfigStr: string,
transformOptions: CacheKeyOptions,
): string {
this.createOrResolveTransformerCfg(transformOptions.config);

return createCacheKey()(fileContent, filePath, this._transformerCfgStr, {
config: transformOptions.config,
instrument: false,
});
}

private createOrResolveTransformerCfg(jestConfig: Config.ProjectConfig): void {
const ccs: CachedConfigSet | undefined = AngularJestTransformer._cachedConfigSets.find(
/**
* Override `ts-jest` method to load our `NgJestConfig` class
*/
configsFor(jestConfig: Config.ProjectConfig): NgJestConfig {
const ccs: CachedConfigSet | undefined = NgJestTransformer._cachedConfigSets.find(
(cs) => cs.jestConfig.value === jestConfig,
);
let ngJestConfig: NgJestConfig;
if (ccs) {
this._transformerCfgStr = ccs.transformerConfig.serialized;
this._ngJestConfig = ccs.ngJestConfig;
this._transformCfgStr = ccs.transformerCfgStr;
ngJestConfig = ccs.ngJestConfig;
} else {
// try to look-it up by stringified version
const stringifiedJestCfg = stringify(jestConfig);
const serializedCcs = AngularJestTransformer._cachedConfigSets.find(
const serializedCcs = NgJestTransformer._cachedConfigSets.find(
(cs) => cs.jestConfig.serialized === stringifiedJestCfg,
);
if (serializedCcs) {
// update the object so that we can find it later
// this happens because jest first calls getCacheKey with stringified version of
// the config, and then it calls the transformer with the proper object
serializedCcs.jestConfig.value = jestConfig;
this._transformerCfgStr = serializedCcs.transformerConfig.serialized;
this._ngJestConfig = serializedCcs.ngJestConfig;
this._transformCfgStr = serializedCcs.transformerCfgStr;
ngJestConfig = serializedCcs.ngJestConfig;
} else {
const ngJestConfig = new NgJestConfig(jestConfig);
const transformerCfg = new JsonObject({
// create the new record in the index
this.logger.info('no matching config-set found, creating a new one');

ngJestConfig = new NgJestConfig(jestConfig);
this._transformCfgStr = new JsonableValue({
...jestConfig,
...ngJestConfig.parsedTsConfig,
});
AngularJestTransformer._cachedConfigSets.push({
jestConfig: new JsonObject(jestConfig),
}).serialized;
NgJestTransformer._cachedConfigSets.push({
jestConfig: new JsonableValue(jestConfig),
ngJestConfig,
transformerConfig: transformerCfg,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
transformerCfgStr: this._transformCfgStr,
});
this._transformerCfgStr = transformerCfg.serialized;
this._ngJestConfig = ngJestConfig;
}
}

return ngJestConfig;
}
}

export = new AngularJestTransformer();
export = new NgJestTransformer();
36 changes: 0 additions & 36 deletions src/utils/json-object.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/utils/json.ts

This file was deleted.

7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,6 @@
slash "^3.0.0"
strip-ansi "^6.0.0"

"@jest/create-cache-key-function@^26.5.0":
version "26.6.2"
resolved "https://registry.yarnpkg.com/@jest/create-cache-key-function/-/create-cache-key-function-26.6.2.tgz#04cf439207a4fd12418d8aee551cddc86f9ac5f5"
integrity sha512-LgEuqU1f/7WEIPYqwLPIvvHuc1sB6gMVbT6zWhin3txYUNYK/kGQrC1F2WR4gR34YlI9bBtViTm5z98RqVZAaw==
dependencies:
"@jest/types" "^26.6.2"

"@jest/environment@^26.6.2":
version "26.6.2"
resolved "https://registry.yarnpkg.com/@jest/environment/-/environment-26.6.2.tgz#ba364cc72e221e79cc8f0a99555bf5d7577cf92c"
Expand Down

0 comments on commit 12189f1

Please sign in to comment.