Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

languageSupport tests #1447

Merged
merged 6 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/generators/__tests__/init-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ describe('init generator', () => {
// Check that all the project files are generated with the correct name
const filePaths = ['package.json', 'README.md', 'src/index.js', '.babelrc'];
assert.file(filePaths.map((file) => join(outputDir, file)));

// eslint-disable-next-line @typescript-eslint/no-var-requires
const output = require(join(outputDir, '.yo-rc.json'));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const config = (Object.entries(output)[0][1] as any).configuration.config.webpackOptions;
expect(config.module.rules).toEqual([
{
test: '/\\.(js|jsx)$/',
include: ["path.resolve(__dirname, 'src')"],
loader: "'babel-loader'",
},
]);
});

it('generates a webpack config that uses Typescript', async () => {
Expand All @@ -168,5 +180,18 @@ describe('init generator', () => {
// Check that all the project files are generated with the correct name
const filePaths = ['package.json', 'README.md', 'src/index.ts', 'tsconfig.json'];
assert.file(filePaths.map((file) => join(outputDir, file)));

// eslint-disable-next-line @typescript-eslint/no-var-requires
const output = require(join(outputDir, '.yo-rc.json'));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const config = (Object.entries(output)[0][1] as any).configuration.config.webpackOptions;
expect(config.module.rules).toEqual([
{
test: '/\\.(ts|tsx)$/',
include: ["path.resolve(__dirname, 'src')"],
loader: "'ts-loader'",
exclude: ['/node_modules/'],
},
]);
});
});
87 changes: 87 additions & 0 deletions packages/generators/__tests__/utils/languageSupport.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import language, { LangType, getBabelLoader, getTypescriptLoader } from '../../lib/utils/languageSupport';
import { CustomGenerator } from '../../lib/types';

describe('languageSupport', () => {
const getMockGenerator = (): CustomGenerator => {
const gen = new CustomGenerator(null, null);
gen.entryOption = "'./path/to/index.js'";
gen.dependencies = [];
gen.configuration = {
config: {
webpackOptions: {
module: {
rules: [],
},
},
},
};
return gen;
};

it('getBabelLoader', () => {
const rule = getBabelLoader(['test1', 'test2']);
expect(rule.loader).toEqual("'babel-loader'");
expect(rule.include).toEqual(["path.resolve(__dirname, 'test1')", "path.resolve(__dirname, 'test2')"]);
});

it('getTypescriptLoader', () => {
const rule = getTypescriptLoader(['test1', 'test2']);
expect(rule.loader).toEqual("'ts-loader'");
expect(rule.include).toEqual(["path.resolve(__dirname, 'test1')", "path.resolve(__dirname, 'test2')"]);
});

it('works with ES6', () => {
const gen = getMockGenerator();
language(gen, LangType.ES6);
expect(gen.entryOption).toEqual("'./path/to/index.js'");
expect(gen.dependencies).toEqual(['babel-loader', '@babel/core', '@babel/preset-env']);

const rules = gen.configuration.config.webpackOptions.module.rules;
expect(rules.length).toEqual(1);
expect(rules[0].include).toEqual(["path.resolve(__dirname, 'path/to')"]);
});

it('works with TypeScript for single entry', () => {
const gen = getMockGenerator();
language(gen, LangType.Typescript);
// this helper preserves the original js entryOption but updates the
// webpack config to use ts
expect(gen.entryOption).toEqual("'./path/to/index.js'");
expect(gen.configuration.config.webpackOptions.entry).toEqual("'./path/to/index.ts'");
expect(gen.dependencies).toEqual(['typescript', 'ts-loader']);
expect(gen.configuration.config.webpackOptions.module.rules.length).toEqual(1);

const rules = gen.configuration.config.webpackOptions.module.rules;
expect(rules.length).toEqual(1);
expect(rules[0].include).toEqual(["path.resolve(__dirname, 'path/to')"]);
});

it('works with TypeScript for multi entry', () => {
const gen = getMockGenerator();
gen.entryOption = {
test1: "'./path/to/test1.js'",
test2: "'./path/to/test2.js'",
test3: "'./different/path/to/test3.js'",
};
language(gen, LangType.Typescript);
// this helper preserves the original js entryOption but updates the
// webpack config to use ts
expect(gen.entryOption).toEqual({
test1: "'./path/to/test1.js'",
test2: "'./path/to/test2.js'",
test3: "'./different/path/to/test3.js'",
});
expect(gen.configuration.config.webpackOptions.entry).toEqual({
test1: "'./path/to/test1.ts'",
test2: "'./path/to/test2.ts'",
test3: "'./different/path/to/test3.ts'",
});
expect(gen.dependencies).toEqual(['typescript', 'ts-loader']);
expect(gen.configuration.config.webpackOptions.module.rules.length).toEqual(1);

const rules = gen.configuration.config.webpackOptions.module.rules;
expect(rules.length).toEqual(1);
// duplicate include paths are removed
expect(rules[0].include).toEqual(["path.resolve(__dirname, 'path/to')", "path.resolve(__dirname, 'different/path/to')"]);
});
});
2 changes: 1 addition & 1 deletion packages/generators/src/utils/languageSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getTypescriptLoader(includeFolders: string[]): Rule {
};
}

export default function language(self, langType: string): void {
export default function language(self: CustomGenerator, langType: string): void {
const entryFolders = getEntryFolders(self);
switch (langType) {
case LangType.ES6:
Expand Down