Skip to content

Commit

Permalink
wow! crazy how much work that took
Browse files Browse the repository at this point in the history
  • Loading branch information
alicewriteswrongs committed May 26, 2023
1 parent cdccedd commit fd7d75e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
11 changes: 5 additions & 6 deletions src/compiler/config/test/load-config.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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),
});
});

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/config/test/validate-config-sourcemap.spec.ts
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -35,14 +35,14 @@ describe('stencil config - sourceMap option', () => {

mock({
[configPath]: mock.load(path.resolve(__dirname, configPath)),
...getMockFSPatch(mock),
});
});

afterEach(() => {
mock.restore();
});


it('sets sourceMap options to true in tsconfig', async () => {
const testConfig = getLoadConfigForTests({ config: { sourceMap: true } });

Expand Down
2 changes: 1 addition & 1 deletion src/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
24 changes: 23 additions & 1 deletion src/testing/testing-utils.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -191,3 +191,25 @@ export async function withSilentWarn<T>(cb: SilentWarnFunc<T>): Promise<T> {
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')),
});

0 comments on commit fd7d75e

Please sign in to comment.