Unverified Commit 91d4aa54 authored by James George's avatar James George Committed by GitHub

tests(create-next-app): prevent catch assertions (#18939)

Prevent `catch` assertions in tests.
parent 019bbb28
...@@ -8,10 +8,7 @@ const cli = require.resolve('create-next-app/dist/index.js') ...@@ -8,10 +8,7 @@ const cli = require.resolve('create-next-app/dist/index.js')
jest.setTimeout(1000 * 60 * 5) jest.setTimeout(1000 * 60 * 5)
const run = (cwd, args, input) => { const run = (args, options) => execa('node', [cli].concat(args), options)
const options = input ? { cwd, input } : { cwd }
return execa('node', [cli].concat(args), options)
}
async function usingTempDir(fn, options) { async function usingTempDir(fn, options) {
const folder = path.join(os.tmpdir(), Math.random().toString(36).substring(2)) const folder = path.join(os.tmpdir(), Math.random().toString(36).substring(2))
...@@ -31,13 +28,9 @@ describe('create next app', () => { ...@@ -31,13 +28,9 @@ describe('create next app', () => {
const pkg = path.join(cwd, projectName, 'package.json') const pkg = path.join(cwd, projectName, 'package.json')
fs.writeFileSync(pkg, '{ "foo": "bar" }') fs.writeFileSync(pkg, '{ "foo": "bar" }')
expect.assertions(1) const res = await run([projectName], { cwd, reject: false })
try { expect(res.exitCode).toBe(1)
await run(cwd, [projectName]) expect(res.stdout).toMatch(/contains files that could conflict/)
} catch (e) {
// eslint-disable-next-line jest/no-try-expect
expect(e.stdout).toMatch(/contains files that could conflict/)
}
}) })
}) })
...@@ -47,7 +40,7 @@ describe('create next app', () => { ...@@ -47,7 +40,7 @@ describe('create next app', () => {
it('empty directory', async () => { it('empty directory', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'empty-directory' const projectName = 'empty-directory'
const res = await run(cwd, [projectName]) const res = await run([projectName], { cwd })
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -63,13 +56,13 @@ describe('create next app', () => { ...@@ -63,13 +56,13 @@ describe('create next app', () => {
it('invalid example name', async () => { it('invalid example name', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'invalid-example-name' const projectName = 'invalid-example-name'
expect.assertions(2) const res = await run([projectName, '--example', 'not a real example'], {
try { cwd,
await run(cwd, [projectName, '--example', 'not a real example']) reject: false,
} catch (e) { })
// eslint-disable-next-line jest/no-try-expect
expect(e.stderr).toMatch(/Could not locate an example named/i) expect(res.exitCode).toBe(1)
} expect(res.stderr).toMatch(/Could not locate an example named/i)
expect( expect(
fs.existsSync(path.join(cwd, projectName, 'package.json')) fs.existsSync(path.join(cwd, projectName, 'package.json'))
).toBeFalsy() ).toBeFalsy()
...@@ -79,7 +72,7 @@ describe('create next app', () => { ...@@ -79,7 +72,7 @@ describe('create next app', () => {
it('valid example', async () => { it('valid example', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'valid-example' const projectName = 'valid-example'
const res = await run(cwd, [projectName, '--example', 'basic-css']) const res = await run([projectName, '--example', 'basic-css'], { cwd })
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -98,11 +91,16 @@ describe('create next app', () => { ...@@ -98,11 +91,16 @@ describe('create next app', () => {
it('should allow example with GitHub URL', async () => { it('should allow example with GitHub URL', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'github-app' const projectName = 'github-app'
const res = await run(cwd, [ const res = await run(
projectName, [
'--example', projectName,
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages', '--example',
]) 'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -123,13 +121,18 @@ describe('create next app', () => { ...@@ -123,13 +121,18 @@ describe('create next app', () => {
it('should allow example with GitHub URL and example-path', async () => { it('should allow example with GitHub URL and example-path', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'github-example-path' const projectName = 'github-example-path'
const res = await run(cwd, [ const res = await run(
projectName, [
'--example', projectName,
'https://github.com/zeit/next-learn-demo/tree/master', '--example',
'--example-path', 'https://github.com/zeit/next-learn-demo/tree/master',
'1-navigate-between-pages', '--example-path',
]) '1-navigate-between-pages',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -150,13 +153,18 @@ describe('create next app', () => { ...@@ -150,13 +153,18 @@ describe('create next app', () => {
it('should use --example-path over the file path in the GitHub URL', async () => { it('should use --example-path over the file path in the GitHub URL', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'github-example-path-2' const projectName = 'github-example-path-2'
const res = await run(cwd, [ const res = await run(
projectName, [
'--example', projectName,
'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages', '--example',
'--example-path', 'https://github.com/zeit/next-learn-demo/tree/master/1-navigate-between-pages',
'1-navigate-between-pages', '--example-path',
]) '1-navigate-between-pages',
],
{
cwd,
}
)
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -180,7 +188,7 @@ describe('create next app', () => { ...@@ -180,7 +188,7 @@ describe('create next app', () => {
it('should fall back to default template', async () => { it('should fall back to default template', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const runExample = (...args) => { const runExample = (...args) => {
const res = run(cwd, args) const res = run(args, { cwd })
function fallbackToTemplate(data) { function fallbackToTemplate(data) {
if ( if (
...@@ -211,7 +219,7 @@ describe('create next app', () => { ...@@ -211,7 +219,7 @@ describe('create next app', () => {
it('should allow an example named default', async () => { it('should allow an example named default', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'default-example' const projectName = 'default-example'
const res = await run(cwd, [projectName, '--example', 'default']) const res = await run([projectName, '--example', 'default'], { cwd })
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
expect( expect(
...@@ -229,43 +237,34 @@ describe('create next app', () => { ...@@ -229,43 +237,34 @@ describe('create next app', () => {
it('should exit if example flag is empty', async () => { it('should exit if example flag is empty', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
try { const projectName = 'no-example-provided'
const projectName = 'no-example-provided' const res = await run([projectName, '--example'], { cwd, reject: false })
await run(cwd, [projectName, '--example']) expect(res.exitCode).toBe(1)
} catch (e) {
// eslint-disable-next-line jest/no-try-expect
expect(e.exitCode).toBe(1)
}
}) })
}) })
it('should exit if the folder is not writable', async () => { it('should exit if the folder is not writable', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'not-writable' const projectName = 'not-writable'
expect.assertions(2) const res = await run([projectName], { cwd, reject: false })
try {
const res = await run(cwd, [projectName]) if (process.platform === 'win32') {
expect(res.exitCode).toBe(0)
if (process.platform === 'win32') { expect(
expect(res.exitCode).toBe(0) fs.existsSync(path.join(cwd, projectName, 'package.json'))
expect( ).toBeTruthy()
fs.existsSync(path.join(cwd, projectName, 'package.json')) return
).toBeTruthy()
}
} catch (e) {
// eslint-disable-next-line jest/no-try-expect
expect(e.exitCode).toBe(1)
// eslint-disable-next-line jest/no-try-expect
expect(e.stderr).toMatch(
/you do not have write permissions for this folder/
)
} }
expect(res.exitCode).toBe(1)
expect(res.stderr).toMatch(
/you do not have write permissions for this folder/
)
}, 0o500) }, 0o500)
}) })
it('should create a project in the current directory', async () => { it('should create a project in the current directory', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const res = await run(cwd, ['.']) const res = await run(['.'], { cwd })
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
const files = ['package.json', 'pages/index.js', '.gitignore'] const files = ['package.json', 'pages/index.js', '.gitignore']
...@@ -278,7 +277,7 @@ describe('create next app', () => { ...@@ -278,7 +277,7 @@ describe('create next app', () => {
it('should ask the user for a name for the project if none supplied', async () => { it('should ask the user for a name for the project if none supplied', async () => {
await usingTempDir(async (cwd) => { await usingTempDir(async (cwd) => {
const projectName = 'test-project' const projectName = 'test-project'
const res = await run(cwd, [], `${projectName}\n`) const res = await run([], { cwd, input: `${projectName}\n` })
expect(res.exitCode).toBe(0) expect(res.exitCode).toBe(0)
const files = ['package.json', 'pages/index.js', '.gitignore'] const files = ['package.json', 'pages/index.js', '.gitignore']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment