diff --git a/tests/default.test.mjs b/tests/default.test.mjs index d79d758..84095a8 100644 --- a/tests/default.test.mjs +++ b/tests/default.test.mjs @@ -1,77 +1,33 @@ -import { describe, it, beforeAll, afterAll, expect } from 'vitest'; +import { describe, it, expect } from 'vitest'; import { join } from 'path'; -import tmp from 'tmp-promise'; -import { execa } from 'execa'; -import copyWithTemplate from '../lib/copy-with-template'; import { existsSync, writeFileSync } from 'fs'; import stripAnsi from 'strip-ansi'; -import { emberCli } from './helpers.mjs'; - -const blueprintPath = join(__dirname, '..'); -const appName = 'fancy-app-in-test'; +import { newProjectWithFixtures } from './helpers.mjs'; describe('basic functionality', function () { - let tmpDir; - - beforeAll(async () => { - tmpDir = await tmp.dir({ unsafeCleanup: true }); - - let emberCliArgs = [ - 'new', - appName, - '-b', - blueprintPath, - '--pnpm', - '--skip-git', - ]; - - await execa(emberCli, emberCliArgs, { - cwd: tmpDir.path, - preferLocal: true, - }); - - // apply the fixture on top of the generated app - copyWithTemplate(join(__dirname, 'fixture'), join(tmpDir.path, appName), { - name: appName, - }); - - // Sync the lints for the fixtures with the project's config - await execa(`pnpm`, ['lint:fix'], { - cwd: join(tmpDir.path, appName), - }); - }); - - afterAll(async () => { - try { - await tmpDir.cleanup(); - } catch { - // if it fails to cleaup we don't want to break CI - } + let project = newProjectWithFixtures({ + fixturePath: join(__dirname, 'fixture'), }); it('verify files', async function () { expect( - !existsSync(join(tmpDir.path, 'app/index.html')), + !existsSync(join(project.dir(), 'app/index.html')), 'the app index.html has been removed', ); expect( - existsSync(join(tmpDir.path, 'index.html')), + existsSync(join(project.dir(), 'index.html')), 'the root index.html has been added', ); }); it('successfully lints', async function () { - let result = await execa('pnpm', ['lint'], { - cwd: join(tmpDir.path, appName), - }); + let result = await project.execa('pnpm', ['lint']); console.log(result.stdout); }); it('successfully builds', async function () { - let result = await execa('pnpm', ['build'], { - cwd: join(tmpDir.path, appName), - }); + let result = await project.execa('pnpm', ['build']); console.log(result.stdout); }); @@ -80,9 +36,7 @@ describe('basic functionality', function () { let result; try { - result = await execa('pnpm', ['test:ember'], { - cwd: join(tmpDir.path, appName), - }); + result = await project.execa('pnpm', ['test:ember']); } catch (err) { console.log(err.stdout, err.stderr); throw err; @@ -99,17 +53,13 @@ describe('basic functionality', function () { }); it('successfully runs tests in dev mode', async function () { - await execa({ - cwd: join(tmpDir.path, appName), - })`pnpm install --save-dev testem http-proxy`; + await project.$`pnpm install --save-dev testem http-proxy`; let appURL; let server; try { - server = execa('pnpm', ['start'], { - cwd: join(tmpDir.path, appName), - }); + server = project.execa('pnpm', ['start']); await new Promise((resolve) => { server.stdout.on('data', (line) => { @@ -125,7 +75,7 @@ describe('basic functionality', function () { }); writeFileSync( - join(tmpDir.path, appName, 'testem-dev.js'), + join(project.dir(), 'testem-dev.js'), `module.exports = { test_page: 'tests/index.html?hidepassed', disable_watching: true, @@ -153,13 +103,12 @@ describe('basic functionality', function () { `, ); - let testResult = await execa( - 'pnpm', - ['testem', '--file', 'testem-dev.js', 'ci'], - { - cwd: join(tmpDir.path, appName), - }, - ); + let testResult = await project.execa('pnpm', [ + 'testem', + '--file', + 'testem-dev.js', + 'ci', + ]); expect(testResult.exitCode).to.eq(0, testResult.output); } finally { server?.kill('SIGINT'); @@ -167,8 +116,6 @@ describe('basic functionality', function () { }); it('successfully optimizes deps', function () { - return execa('pnpm', ['vite', 'optimize', '--force'], { - cwd: join(tmpDir.path, appName), - }); + return project.execa('pnpm', ['vite', 'optimize', '--force']); }); }); diff --git a/tests/helpers.mjs b/tests/helpers.mjs index 412b4c9..9c994e1 100644 --- a/tests/helpers.mjs +++ b/tests/helpers.mjs @@ -1,7 +1,77 @@ +import assert from 'node:assert'; +import { join } from 'node:path'; + +import { beforeAll, afterAll } from 'vitest'; import { sync as resolveBinSync } from 'resolve-bin'; +import { execa } from 'execa'; +import tmp from 'tmp-promise'; + +import copyWithTemplate from '../lib/copy-with-template'; function findEmber() { return resolveBinSync('ember-cli', { executable: 'ember' }); } export const emberCli = findEmber(); + +const blueprintPath = join(__dirname, '..'); +const appName = 'fancy-app-in-test'; + +export function newProjectWithFixtures({ + flags = [], + fixturePath, + name = appName, +} = {}) { + let tmpDir; + + assert(fixturePath, `a fixturePath is required`); + + beforeAll(async () => { + tmpDir = await tmp.dir({ unsafeCleanup: true }); + + let emberCliArgs = [ + 'new', + name, + '-b', + blueprintPath, + '--pnpm', + '--skip-git', + ...flags, + ]; + + await execa(emberCli, emberCliArgs, { + cwd: tmpDir.path, + preferLocal: true, + }); + + // apply the fixture on top of the generated app + copyWithTemplate(join(__dirname, 'fixture'), join(tmpDir.path, name), { + name, + }); + + // Sync the lints for the fixtures with the project's config + await execa(`pnpm`, ['lint:fix'], { + cwd: join(tmpDir.path, name), + }); + }); + + afterAll(async () => { + try { + await tmpDir.cleanup(); + } catch { + // if it fails to cleaup we don't want to break CI + } + }); + + return { + tmpDir: () => tmpDir.path, + appName: () => name, + dir: () => join(tmpDir.path, name), + $: (...args) => execa({ cwd: join(tmpDir.path, name) })(...args), + execa: (program, args, options = {}) => + execa(program, args, { + cwd: join(tmpDir.path, name), + ...options, + }), + }; +}