Skip to content

Commit

Permalink
chore(test): increase timeout and check all files in intergration test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Dec 2, 2021
1 parent f559dd0 commit ca80708
Showing 1 changed file with 46 additions and 38 deletions.
84 changes: 46 additions & 38 deletions test/integration/create-react-app/index.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
'use strict';

const execa = require('execa');
const { mkdirp, writeFileSync, existsSync } = require('fs-extra');
const { mkdirp, writeFileSync, existsSync, readdirSync } = require('fs-extra');
const { join } = require('path');
const { rmSync } = require('fs');

const cli = require.resolve('create-react-app/index.js');

jest.setTimeout(1000 * 60 * 5);
jest.setTimeout(1000 * 60 * 10);

const projectName = 'test-app';
const genPath = join(__dirname, projectName);

const generatedFiles = [
'.gitignore',
'README.md',
'node_modules',
'package.json',
'public',
'src',
'package-lock.json',
];
Expand Down Expand Up @@ -46,10 +49,17 @@ const run = async (args, options) => {
const childProcessResult = await result;
process.stdout.write(`ExitCode: ${childProcessResult.exitCode}\n`);
process.stdout.write('::endgroup::\n');
return childProcessResult;
const files = existsSync(genPath)
? readdirSync(genPath).filter(f => existsSync(join(genPath, f)))
: null;
return {
...childProcessResult,
files,
};
};

const genFileExists = f => existsSync(join(genPath, f));
const expectAllFiles = (arr1, arr2) =>
expect([...arr1].sort()).toEqual([...arr2].sort());

describe('create-react-app', () => {
it('check yarn installation', async () => {
Expand All @@ -60,21 +70,22 @@ describe('create-react-app', () => {
});

it('asks to supply an argument if none supplied', async () => {
const { exitCode, stderr } = await run([], { reject: false });
const { exitCode, stderr, files } = await run([], { reject: false });

// Assertions
expect(exitCode).toBe(1);
expect(stderr).toContain('Please specify the project directory');
expect(files).toBe(null);
});

it('creates a project on supplying a name as the argument', async () => {
const { exitCode } = await run([projectName], { cwd: __dirname });
const { exitCode, files } = await run([projectName], { cwd: __dirname });

// Assert for exit code
expect(exitCode).toBe(0);

// Assert for the generated files
generatedFiles.forEach(file => expect(genFileExists(file)).toBeTruthy());
expectAllFiles(files, generatedFiles);
});

it('warns about conflicting files in path', async () => {
Expand All @@ -85,7 +96,7 @@ describe('create-react-app', () => {
const pkgJson = join(genPath, 'package.json');
writeFileSync(pkgJson, '{ "foo": "bar" }');

const { exitCode, stdout } = await run([projectName], {
const { exitCode, stdout, files } = await run([projectName], {
cwd: __dirname,
reject: false,
});
Expand All @@ -97,57 +108,54 @@ describe('create-react-app', () => {
expect(stdout).toContain(
`The directory ${projectName} contains files that could conflict`
);

// Existing file is still there
expectAllFiles(files, ['package.json']);
});

it('creates a project in the current directory', async () => {
// Create temporary directory
await mkdirp(genPath);

// Create a project in the current directory
const { exitCode } = await run(['.'], { cwd: genPath });
const { exitCode, files } = await run(['.'], { cwd: genPath });

// Assert for exit code
expect(exitCode).toBe(0);

// Assert for the generated files
generatedFiles.forEach(file => expect(genFileExists(file)).toBeTruthy());
expectAllFiles(files, generatedFiles);
});

it(
'uses yarn as the package manager',
async () => {
const { exitCode } = await run([projectName], {
cwd: __dirname,
env: { npm_config_user_agent: 'yarn' },
});

// Assert for exit code
expect(exitCode).toBe(0);

// Assert for the generated files
const generatedFilesWithYarn = [
...generatedFiles.filter(file => file !== 'package-lock.json'),
'yarn.lock',
];

generatedFilesWithYarn.forEach(file =>
expect(genFileExists(file)).toBeTruthy()
);
},
1000 * 60 * 10
);

it('creates a project based on the typescript template', async () => {
const { exitCode } = await run([projectName, '--template', 'typescript'], {
it('uses yarn as the package manager', async () => {
const { exitCode, files } = await run([projectName], {
cwd: __dirname,
env: { npm_config_user_agent: 'yarn' },
});

// Assert for exit code
expect(exitCode).toBe(0);

// Assert for the generated files
[...generatedFiles, 'tsconfig.json'].forEach(file =>
expect(genFileExists(file)).toBeTruthy()
const generatedFilesWithYarn = generatedFiles.map(file =>
file === 'package-lock.json' ? 'yarn.lock' : file
);

expectAllFiles(files, generatedFilesWithYarn);
});

it('creates a project based on the typescript template', async () => {
const { exitCode, files } = await run(
[projectName, '--template', 'typescript'],
{
cwd: __dirname,
}
);

// Assert for exit code
expect(exitCode).toBe(0);

// Assert for the generated files
expectAllFiles(files, [...generatedFiles, 'tsconfig.json']);
});
});

0 comments on commit ca80708

Please sign in to comment.