Skip to content

Commit

Permalink
test: Expanding 'create' CLI test
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Jun 8, 2022
1 parent 5cae21c commit 2287be2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
63 changes: 44 additions & 19 deletions packages/cli/tests/create.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { readFile } = require('fs').promises;
const { relative, resolve } = require('path');
const { access, readFile } = require('fs').promises;
const { join, relative } = require('path');
const { create } = require('./lib/cli');
const { expand } = require('./lib/utils');
const snapshots = require('./images/create');
const shell = require('shelljs');

describe('preact create', () => {
it(`scaffolds the 'default' official template`, async () => {
it('scaffolds the `default` official template', async () => {
let dir = await create('default');

let output = await expand(dir).then(arr => {
Expand All @@ -15,29 +16,53 @@ describe('preact create', () => {
expect(output.sort()).toEqual(snapshots.default);
});

it(`should use template.html from the github repo`, async () => {
it('should use template.html from the github repo', async () => {
let dir = await create('netlify');

const templateFilePath = resolve(__dirname, dir, 'src', 'template.html');
const template = await readFile(templateFilePath, 'utf8');

const template = await readFile(join(dir, 'src/template.html'), 'utf8');
expect(template.includes('twitter:card')).toEqual(true);
});

it(`should have 'apple-touch-icon' meta tag`, async () => {
let dir = await create('simple');
describe('CLI Options', () => {
it('--name', async () => {
let dir = await create('simple', { name: 'renamed' });
const packageJson = await readFile(join(dir, 'package.json'), 'utf8');

const templateFilePath = resolve(__dirname, dir, 'src', 'template.html');
const template = await readFile(templateFilePath, 'utf8');
expect(JSON.parse(packageJson).name).toBe('renamed');

expect(template.includes('apple-touch-icon')).toEqual(true);
});
// @ts-ignore
const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
await create('simple', { name: '*()@!#!$-Invalid-Name' });
expect(mockExit).toHaveBeenCalledWith(1);
mockExit.mockRestore();
});

it('should fail given an invalid name', async () => {
// @ts-ignore
const exit = jest.spyOn(process, 'exit').mockImplementation(() => {});
await create('simple', '*()@!#!$-Invalid-Name');
it('--git', async () => {
let dir = await create('simple', { git: true });
expect(await access(join(dir, '.git'))).toBeUndefined();

expect(exit).toHaveBeenCalledWith(1);
dir = await create('simple', { git: false });
await expect(access(join(dir, '.git'))).rejects.toThrow(
'no such file or directory'
);
});

// TODO (maybe): All of the official templates hard-code the license,
// rather than use template variable (`{{ license }}`), so we can't test this
//it('--license', async () => {
// let dir = await create('simple', { license: 'GPL' });
// const packageJson = await readFile(join(dir, 'package.json'), 'utf8');

// expect(JSON.parse(packageJson).license).toBe('GPL');
//});

it('--invalid-arg', () => {
const { code, stderr } = shell.exec(
`node ${join(__dirname, '../lib/index.js')} create --invalid-arg`
);
expect(stderr).toMatch(
"Invalid argument '--invalid-arg' passed to create."
);
expect(code).toBe(1);
});
});
});
8 changes: 4 additions & 4 deletions packages/cli/tests/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const argv = {
'inline-css': true,
};

exports.create = async function (template, name) {
exports.create = async function (template, options) {
let dest = await tmpDir();
name = name || `test-${template}`;

await cmd.create(template, dest, { name, cwd: '.' });
let opts = Object.assign({ name: `test-${template}`, cwd: '.' }, options);
await cmd.create(template, dest, opts);

return dest;
};
Expand All @@ -32,7 +32,7 @@ exports.build = async function (cwd, options, installNodeModules = false) {
shell.exec(`npm --prefix ${cwd} i`);
}

let opts = Object.assign({}, { cwd }, argv, options);
let opts = Object.assign({ cwd }, argv, options);
return await cmd.build(opts.src, opts);
};

Expand Down

0 comments on commit 2287be2

Please sign in to comment.