Skip to content

Commit

Permalink
Extract a helpers.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Sep 26, 2024
1 parent 1743eb5 commit a4ed023
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 50 deletions.
65 changes: 15 additions & 50 deletions tests/default.test.mjs
Original file line number Diff line number Diff line change
@@ -1,75 +1,40 @@
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';

const blueprintPath = join(__dirname, '..');
const appName = 'fancy-app-in-test';

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('ember', 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),
});
});
import { newProjectWithFixtures } from './helpers';

afterAll(async () => {
try {
await tmpDir.cleanup();
} catch {
// if it fails to cleaup we don't want to break CI
}
describe('basic functionality', function () {
let project = newProjectWithFixtures({
fixturePath: join(__dirname, 'fixture'),
});

it('verify files', async function () {
expect(
!existsSync(join(tmpDir.path, 'app/index.html')),
!existsSync(join(project.tmpDir(), 'app/index.html')),
'the app index.html has been removed',
);
expect(
existsSync(join(tmpDir.path, 'index.html')),
existsSync(join(project.tmpDir(), '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),
cwd: join(project.tmpDir(), appName),
});

console.log(result.stdout);
});

it('successfully builds', async function () {
let result = await execa('pnpm', ['build'], {
cwd: join(tmpDir.path, appName),
cwd: join(project.tmpDir(), appName),
});

console.log(result.stdout);
Expand All @@ -80,7 +45,7 @@ describe('basic functionality', function () {

try {
result = await execa('pnpm', ['test:ember'], {
cwd: join(tmpDir.path, appName),
cwd: join(project.tmpDir(), appName),
});
} catch (err) {
console.log(err.stdout, err.stderr);
Expand All @@ -99,15 +64,15 @@ describe('basic functionality', function () {

it('successfully runs tests in dev mode', async function () {
await execa({
cwd: join(tmpDir.path, appName),
cwd: join(project.tmpDir(), appName),
})`pnpm install --save-dev testem http-proxy`;
let appURL;

let server;

try {
server = execa('pnpm', ['start'], {
cwd: join(tmpDir.path, appName),
cwd: join(project.tmpDir(), appName),
});

await new Promise((resolve) => {
Expand All @@ -124,7 +89,7 @@ describe('basic functionality', function () {
});

writeFileSync(
join(tmpDir.path, appName, 'testem-dev.js'),
join(project.tmpDir(), appName, 'testem-dev.js'),
`module.exports = {
test_page: 'tests/index.html?hidepassed',
disable_watching: true,
Expand Down Expand Up @@ -156,7 +121,7 @@ describe('basic functionality', function () {
'pnpm',
['testem', '--file', 'testem-dev.js', 'ci'],
{
cwd: join(tmpDir.path, appName),
cwd: join(project.tmpDir(), appName),
},
);
expect(testResult.exitCode).to.eq(0, testResult.output);
Expand All @@ -167,7 +132,7 @@ describe('basic functionality', function () {

it('successfully optimizes deps', function () {
return execa('pnpm', ['vite', 'optimize', '--force'], {
cwd: join(tmpDir.path, appName),
cwd: join(project.tmpDir(), appName),
});
});
});
63 changes: 63 additions & 0 deletions tests/helpers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import assert from 'node:assert';
import { join } from 'node:path';

import { beforeAll, afterAll } from 'vitest';
import tmp from 'tmp-promise';
import { execa } from 'execa';
import copyWithTemplate from '../lib/copy-with-template';

const blueprintPath = join(__dirname, '..');
const appName = 'fancy-app-in-test';

export function newProjectWithFixtures({
flags = [],
fixturePath,
name = appName,
} = {}) {
let tmpDir;
let emberCli = join(__dirname, '../node_modules/ember-cli/bin/ember');

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,
};
}

0 comments on commit a4ed023

Please sign in to comment.