diff --git a/src/compiler/config/test/load-config.spec.ts b/src/compiler/config/test/load-config.spec.ts index c9515b642654..d30e4f23f6fd 100644 --- a/src/compiler/config/test/load-config.spec.ts +++ b/src/compiler/config/test/load-config.spec.ts @@ -1,7 +1,8 @@ +import { getMockFSPatch } from '@stencil/core/testing'; import { createNodeSys } from '@sys-api-node'; -import path from 'path'; -import mock from 'mock-fs'; import fs from 'fs'; +import mock from 'mock-fs'; +import path from 'path'; import { ConfigFlags } from '../../../cli/config-flags'; import type * as d from '../../../declarations'; @@ -41,6 +42,7 @@ describe('load config', () => { [configPath2]: mock.load(path.resolve(__dirname, configPath2)), 'fixtures/tsconfig.json': tsconfig, [noTsConfigPath]: mock.load(path.resolve(__dirname, configPath)), + ...getMockFSPatch(mock), }); }); @@ -101,10 +103,7 @@ describe('load config', () => { }); it('creates a tsconfig file when "initTsConfig" set', async () => { - const tsconfigPath = path.resolve( - path.dirname(noTsConfigPath), - "tsconfig.json" - ); + const tsconfigPath = path.resolve(path.dirname(noTsConfigPath), 'tsconfig.json'); expect(fs.existsSync(tsconfigPath)).toBe(false); const loadedConfig = await loadConfig({ initTsConfig: true, configPath: noTsConfigPath }); expect(fs.existsSync(tsconfigPath)).toBe(true); diff --git a/src/compiler/config/test/validate-config-sourcemap.spec.ts b/src/compiler/config/test/validate-config-sourcemap.spec.ts index 324d92c0d0fc..2e6120ff81f4 100644 --- a/src/compiler/config/test/validate-config-sourcemap.spec.ts +++ b/src/compiler/config/test/validate-config-sourcemap.spec.ts @@ -1,15 +1,15 @@ import { mockLoadConfigInit } from '@stencil/core/testing'; +import { getMockFSPatch } from '@stencil/core/testing'; import { createNodeSys } from '@sys-api-node'; -import path from 'path'; import mock from 'mock-fs'; -import fs from 'fs'; +import path from 'path'; import type * as d from '../../../declarations'; import { loadConfig } from '../load-config'; describe('stencil config - sourceMap option', () => { const fixturesDir = 'fixtures'; - const configPath = path.join(fixturesDir, "stencil.config.ts"); + const configPath = path.join(fixturesDir, 'stencil.config.ts'); let sys: d.CompilerSystem; /** @@ -35,6 +35,7 @@ describe('stencil config - sourceMap option', () => { mock({ [configPath]: mock.load(path.resolve(__dirname, configPath)), + ...getMockFSPatch(mock), }); }); @@ -42,7 +43,6 @@ describe('stencil config - sourceMap option', () => { mock.restore(); }); - it('sets sourceMap options to true in tsconfig', async () => { const testConfig = getLoadConfigForTests({ config: { sourceMap: true } }); diff --git a/src/testing/index.ts b/src/testing/index.ts index e42d7d8e3166..b3956beb9f6b 100644 --- a/src/testing/index.ts +++ b/src/testing/index.ts @@ -27,5 +27,5 @@ export { E2EElement, E2EPage, newE2EPage } from './puppeteer'; export { newSpecPage } from './spec-page'; export { transpile } from './test-transpile'; export { createTesting } from './testing'; -export { shuffleArray } from './testing-utils'; +export { getMockFSPatch, shuffleArray } from './testing-utils'; export type { EventSpy, SpecPage, Testing } from '@stencil/core/internal'; diff --git a/src/testing/testing-utils.ts b/src/testing/testing-utils.ts index 866c58a7540e..a53b299b5b19 100644 --- a/src/testing/testing-utils.ts +++ b/src/testing/testing-utils.ts @@ -1,6 +1,6 @@ import type * as d from '@stencil/core/internal'; import { isOutputTargetDistLazy, isOutputTargetWww } from '@utils'; -import { join, relative } from 'path'; +import path, { join, relative } from 'path'; import { InMemoryFileSystem } from '../compiler/sys/in-memory-fs'; @@ -191,3 +191,25 @@ export async function withSilentWarn(cb: SilentWarnFunc): Promise { console.warn = realWarn; return retVal; } + +/** + * This is our custom patched version of `mock-fs`. This replaces the node.js + * implementation of `fs` with a separate one which does filesystem operations + * against an in-memory filesystem instead of against the disk. + * + * The 'patch' consists of adding files to the in-memory filesystem from the + * disk which are sometimes required by Jest between the `beforeEach` and + * `afterEach` calls in a test suite -- without adding these files to the + * in-memory filesystem the runtime require by Jest will fail. + * + * @param mock a `mock-fs` module, as imported in the particular test file + * where you want to mock the filesystem. + * @returns a filesystem manifest suitable for mocking out runtime + * dependencies of Jest + */ +export const getMockFSPatch = (mock: any) => ({ + 'node_modules/write-file-atomic': mock.load(path.resolve(process.cwd(), 'node_modules', 'write-file-atomic')), + 'node_modules/imurmurhash': mock.load(path.resolve(process.cwd(), 'node_modules', 'imurmurhash')), + 'node_modules/is-typedarray': mock.load(path.resolve(process.cwd(), 'node_modules', 'is-typedarray')), + 'node_modules/typedarray-to-buffer': mock.load(path.resolve(process.cwd(), 'node_modules', 'typedarray-to-buffer')), +});