Skip to content

Commit

Permalink
alternative for addressing test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alicewriteswrongs committed May 15, 2023
1 parent c894733 commit cc88044
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'@stencil/core/mock-doc': '<rootDir>/mock-doc/index.cjs',
'@stencil/core/testing': '<rootDir>/testing/index.js',
'@utils': '<rootDir>/src/utils',
'^typescript$': '<rootDir>/scripts/build/typescript-modified-for-jest.js',
},
coverageDirectory: './coverage/',
coverageReporters: ['json', 'lcov', 'text', 'clover'],
Expand Down
3 changes: 3 additions & 0 deletions scripts/bundles/plugins/typescript-source-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ async function bundleTypeScriptSource(tsPath: string, opts: BuildOptions): Promi

code = code.replace(TS_PROP_DEFINER, TS_PROP_DEFINER_RECONFIGURABLE);

const jestTypesciptFilename = join(opts.scriptsBuildDir, 'typescript-modified-for-jest.js');
await fs.writeFile(jestTypesciptFilename, code);

// Here we transform the TypeScript source from a commonjs to an ES module.
// We do this so that we can add an import from the `@environment` module.

Expand Down
26 changes: 6 additions & 20 deletions src/compiler/transformers/test/map-imports-to-path-aliases.spec.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import type { OutputTargetDistCollection } from '@stencil/core/declarations';
import { mockValidatedConfig } from '@stencil/core/testing';
import { Extension } from 'typescript';
import * as ts from 'typescript';
import ts, { Extension } from 'typescript';

import { ValidatedConfig } from '../../../internal';
import { mapImportsToPathAliases } from '../map-imports-to-path-aliases';
import { transpileModule } from './transpile';

let resolveModuleNameSpy: jest.Mock<ReturnType<typeof ts.resolveModuleName>, Parameters<typeof ts.resolveModuleName>>;

// we need to mock typescript instead of being able to spy on
// `resolveModuleName` because as of TS 5.0 the default export is an object
// where methods are set on it using `Object.defineProperty` but they are
// _not_ configurable. This means that `jest.spyOn` can't overwrite them with
// a spy function!
jest.mock('typescript', () => {
const original = jest.requireActual('typescript');
return {
...original,
resolveModuleName: jest.fn((moduleName, containingFile, compilerOptions, moduleResolutionHost) => {
return resolveModuleNameSpy(moduleName, containingFile, compilerOptions, moduleResolutionHost);
}),
};
});

describe('mapImportsToPathAliases', () => {
let module: ReturnType<typeof transpileModule>;
let config: ValidatedConfig;
let resolveModuleNameSpy: jest.SpyInstance<
ReturnType<typeof ts.resolveModuleName>,
Parameters<typeof ts.resolveModuleName>
>;
let outputTarget: OutputTargetDistCollection;

beforeEach(() => {
config = mockValidatedConfig({ tsCompilerOptions: {} });

resolveModuleNameSpy = jest.fn();
resolveModuleNameSpy = jest.spyOn(ts, 'resolveModuleName');

outputTarget = {
type: 'dist-collection',
Expand Down

0 comments on commit cc88044

Please sign in to comment.