Skip to content

Commit

Permalink
feat(misc): support customizing cypress webpack config (#2503)
Browse files Browse the repository at this point in the history
Co-authored-by: Spencer Elliott <me@elliottsj.com>
  • Loading branch information
vsavkin and elliottsj committed Feb 19, 2020
1 parent 647a657 commit d27cb3a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
42 changes: 41 additions & 1 deletion packages/cypress/src/plugins/preprocessor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { getWebpackConfig } from './preprocessor';
import { getWebpackConfig, preprocessTypescript } from './preprocessor';
jest.mock('@cypress/webpack-preprocessor', () => {
return jest.fn(() => (...args) => Promise.resolve());
});
jest.mock('tsconfig-paths-webpack-plugin');
import * as wp from '@cypress/webpack-preprocessor';
import TsConfigPathsPlugin from 'tsconfig-paths-webpack-plugin';

describe('getWebpackConfig', () => {
Expand Down Expand Up @@ -66,3 +70,39 @@ describe('getWebpackConfig', () => {
expect(callback).toHaveBeenCalledWith(null, 'commonjs @nestjs/core');
});
});

describe('preprocessTypescript', () => {
it('should work if no customizer is passed', async () => {
const preprocessor = preprocessTypescript({
env: {
tsConfig: './tsconfig.json'
}
});
await preprocessor('arg0', 'arg1');
expect(wp).toBeCalled();
expect(
wp.mock.calls[wp.mock.calls.length - 1][0].webpackOptions.resolve
.extensions
).toEqual(['.ts', '.tsx', '.mjs', '.js', '.jsx']);
});

it('should support customizing the webpack config', async () => {
const preprocessor = preprocessTypescript(
{
env: {
tsConfig: './tsconfig.json'
}
},
webpackConfig => {
webpackConfig.resolve.extensions.push('.mdx');
return webpackConfig;
}
);
await preprocessor('arg0', 'arg1');
expect(wp).toBeCalled();
expect(
wp.mock.calls[wp.mock.calls.length - 1][0].webpackOptions.resolve
.extensions
).toEqual(['.ts', '.tsx', '.mjs', '.js', '.jsx', '.mdx']);
});
});
15 changes: 10 additions & 5 deletions packages/cypress/src/plugins/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { TsconfigPathsPlugin } from 'tsconfig-paths-webpack-plugin';
import * as nodeExternals from 'webpack-node-externals';
import * as ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';

export function preprocessTypescript(config: any) {
export function preprocessTypescript(
config: any,
customizeWebpackConfig?: (webpackConfig: any) => any
) {
if (!config.env.tsConfig) {
throw new Error(
'Please provide an absolute path to a tsconfig.json as cypressConfig.env.tsConfig'
);
}

return async (...args) =>
wp({
webpackOptions: getWebpackConfig(config)
})(...args);
return async (...args) => {
const webpackOptions = customizeWebpackConfig
? customizeWebpackConfig(getWebpackConfig(config))
: getWebpackConfig(config);
return wp({ webpackOptions })(...args);
};
}

export function getWebpackConfig(config: any) {
Expand Down

0 comments on commit d27cb3a

Please sign in to comment.