|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const execa = require('execa'); |
| 4 | +const { mkdirp, remove, writeFileSync } = require('fs-extra'); |
| 5 | +const { join } = require('path'); |
| 6 | + |
| 7 | +const cli = require.resolve('create-react-app/index.js'); |
| 8 | + |
| 9 | +jest.setTimeout(1000 * 60 * 5); |
| 10 | + |
| 11 | +const projectName = 'test-app'; |
| 12 | +const genPath = join(__dirname, projectName); |
| 13 | + |
| 14 | +const generatedFiles = ['.gitignore', 'package.json', 'src', 'yarn.lock']; |
| 15 | + |
| 16 | +beforeEach(() => remove(genPath)); |
| 17 | +afterAll(() => remove(genPath)); |
| 18 | + |
| 19 | +const run = (args, options) => execa('node', [cli].concat(args), options); |
| 20 | + |
| 21 | +describe('create-react-app', () => { |
| 22 | + it('asks to supply an argument if none supplied', async () => { |
| 23 | + const { stderr } = await run([], { reject: false }); |
| 24 | + expect(stderr).toContain('Please specify the project directory'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('creates a project on supplying a name as the argument', async () => { |
| 28 | + await run([projectName], { cwd: __dirname }); |
| 29 | + |
| 30 | + // Assert for the generated files |
| 31 | + generatedFiles.forEach(file => expect(join(genPath, file)).toBeTruthy()); |
| 32 | + }); |
| 33 | + |
| 34 | + it('warns about conflicting files in path', async () => { |
| 35 | + // Create the temporary directory |
| 36 | + await mkdirp(genPath); |
| 37 | + |
| 38 | + // Create a package.json file |
| 39 | + const pkgJson = join(genPath, 'package.json'); |
| 40 | + writeFileSync(pkgJson, '{ "foo": "bar" }'); |
| 41 | + |
| 42 | + const { stdout } = await run([projectName], { |
| 43 | + cwd: __dirname, |
| 44 | + reject: false, |
| 45 | + }); |
| 46 | + |
| 47 | + // Assert for the expected message |
| 48 | + expect(stdout).toContain( |
| 49 | + `The directory ${projectName} contains files that could conflict` |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it('creates a project in the current directory', async () => { |
| 54 | + // Create temporary directory |
| 55 | + await mkdirp(genPath); |
| 56 | + |
| 57 | + // Create a project in the current directory |
| 58 | + await run(['.'], { cwd: genPath }); |
| 59 | + |
| 60 | + // Assert for the generated files |
| 61 | + generatedFiles.forEach(file => expect(join(genPath, file)).toBeTruthy()); |
| 62 | + }); |
| 63 | + |
| 64 | + it('uses npm as the package manager', async () => { |
| 65 | + await run([projectName, '--use-npm'], { |
| 66 | + cwd: __dirname, |
| 67 | + }); |
| 68 | + |
| 69 | + // Assert for the generated files |
| 70 | + const generatedFilesWithNpm = [ |
| 71 | + ...generatedFiles.filter(file => file !== 'yarn.lock'), |
| 72 | + 'package-lock.json', |
| 73 | + ]; |
| 74 | + |
| 75 | + generatedFilesWithNpm.forEach(file => |
| 76 | + expect(join(genPath, file)).toBeTruthy() |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('creates a project based on the typescript template', async () => { |
| 81 | + await run([projectName, '--template', 'typescript'], { |
| 82 | + cwd: __dirname, |
| 83 | + }); |
| 84 | + |
| 85 | + // Assert for the generated files |
| 86 | + [...generatedFiles, 'tsconfig.json'].forEach(file => |
| 87 | + expect(join(genPath, file)).toBeTruthy() |
| 88 | + ); |
| 89 | + }); |
| 90 | +}); |
0 commit comments