Skip to content

Commit

Permalink
Merge pull request #87 from embroider-build/extract-helpers
Browse files Browse the repository at this point in the history
Extract project creation util so that multiple test files can share the "ember new + fixture" stuff
  • Loading branch information
mansona authored Sep 30, 2024
2 parents 2fb32d8 + 6bb8958 commit 0debaa7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 72 deletions.
91 changes: 19 additions & 72 deletions tests/default.test.mjs
Original file line number Diff line number Diff line change
@@ -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);
});
Expand All @@ -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;
Expand All @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -153,22 +103,19 @@ 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');
}
});

it('successfully optimizes deps', function () {
return execa('pnpm', ['vite', 'optimize', '--force'], {
cwd: join(tmpDir.path, appName),
});
return project.execa('pnpm', ['vite', 'optimize', '--force']);
});
});
70 changes: 70 additions & 0 deletions tests/helpers.mjs
Original file line number Diff line number Diff line change
@@ -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,
}),
};
}

0 comments on commit 0debaa7

Please sign in to comment.