Skip to content

Commit

Permalink
fix: babel-config + adds test
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 13, 2018
1 parent d697c0b commit 12146c3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TsJestGlobalOptions } from '../types';
import { TsJestGlobalOptions, BabelConfig } from '../types';
import { resolve } from 'path';

export function filePath(relPath: string): string {
Expand Down Expand Up @@ -29,7 +29,7 @@ describe('hello', function () {
export function htmlSource() {
return `
<div>
<span>some text with \`backtilt\`</span>
<span>some text with \`backtick\`</span>
</div>
`;
}
Expand Down Expand Up @@ -71,3 +71,11 @@ export function jestConfig<T extends jest.ProjectConfig>(
}
return res;
}

export function babelConfig<T extends BabelConfig>(options?: BabelConfig): T {
return {
...options,
presets: [...(options && options.presets)],
plugins: [...(options && options.plugins)],
} as any;
}
53 changes: 53 additions & 0 deletions src/utils/babel-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as fakers from '../__helpers__/fakers';
import * as babelCfg from './babel-config';
import { BabelConfig } from '../types';

describe('extend', () => {
let base!: BabelConfig;

beforeEach(() => {
base = fakers.babelConfig({
presets: ['preset-1'],
plugins: ['plugin-1'],
minified: true,
});
});

it('should be a copy', () => {
const extended = babelCfg.extend(base);
expect(extended).toEqual(base);
expect(extended).not.toBe(base);
expect(extended.presets).not.toBe(base.presets);
expect(extended.plugins).not.toBe(base.plugins);
});

it('should extend correctly', () => {
const extension: BabelConfig = {
minified: false,
envName: 'test',
plugins: ['plugin-2'],
presets: ['preset-2'],
};
const extended = babelCfg.extend(base, extension);
expect(extended).not.toBe(base);
expect(extended).not.toBe(extension);
expect(extended.presets).not.toBe(base.presets);
expect(extended.presets).not.toBe(extension.presets);
expect(extended.plugins).not.toBe(base.plugins);
expect(extended.plugins).not.toBe(extension.plugins);
expect(extended).toMatchInlineSnapshot(`
Object {
"envName": "test",
"minified": false,
"plugins": Array [
"plugin-1",
"plugin-2",
],
"presets": Array [
"preset-1",
"preset-2",
],
}
`);
});
});
9 changes: 6 additions & 3 deletions src/utils/babel-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ export function extend(
// tslint:disable-next-line:trailing-comma
...others: BabelConfig[]
): BabelConfig {
const res = Object.assign(base, extend, ...others, {
const res = {
...base,
...extend,
...others,
presets: base.presets ? base.presets.slice() : [],
plugins: extend.plugins ? extend.plugins.slice() : [],
});
plugins: base.plugins ? base.plugins.slice() : [],
};
others.unshift(extend);
others.forEach(other => {
if (other.presets) {
Expand Down

0 comments on commit 12146c3

Please sign in to comment.