-
-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add CLI tests for all subcommands & most of their flags (#1738)
* test: Add CLI tests for all commands & most flags * chore: Remove commented-out `watch` tests
- Loading branch information
1 parent
735b72e
commit 12e230d
Showing
12 changed files
with
310 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
declare global { | ||
const __webpack_public_path__: string; | ||
namespace jest { | ||
interface Matchers<R> { | ||
toBeCloseInSize(receivedSize: number, expectedSize: number): R; | ||
toFindMatchingKey(receivedKey: string): R; | ||
} | ||
declare const __webpack_public_path__: string; | ||
|
||
declare namespace jest { | ||
interface Matchers<R> { | ||
toBeCloseInSize(receivedSize: number, expectedSize: number): R; | ||
toFindMatchingKey(receivedKey: string): R; | ||
} | ||
} | ||
|
||
export {}; | ||
// Modified from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/shelljs/index.d.ts | ||
declare module 'shelljs' { | ||
const shell: { | ||
cd: (string) => void; | ||
exec: (string) => { stdout: string; stderr: string; code: number }; | ||
}; | ||
export = shell; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const { join } = require('path'); | ||
const shell = require('shelljs'); | ||
const { subject } = require('./lib/output'); | ||
|
||
describe('preact info', () => { | ||
it('--src', async () => { | ||
let dir = await subject('minimal'); | ||
// env-info does not pick up on symlinks, so we need to install deps | ||
shell.exec(`npm --prefix ${dir} i preact preact-render-to-string`); | ||
|
||
const _cwd = process.cwd(); | ||
|
||
shell.cd(dir); | ||
const { code, stdout } = shell.exec( | ||
`node ${join(__dirname, '../src/index.js')} info` | ||
); | ||
['OS', 'Node', 'preact', 'preact-render-to-string'].forEach(label => { | ||
expect(stdout).toMatch(`${label}:`); | ||
}); | ||
expect(code).toBe(0); | ||
|
||
shell.cd(_cwd); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,49 @@ | ||
const { join } = require('path'); | ||
const { mkdir, symlink } = require('fs').promises; | ||
const { mkdir } = require('fs').promises; | ||
const shell = require('shelljs'); | ||
const cmd = require('../../src/commands'); | ||
const { tmpDir } = require('./output'); | ||
const shell = require('shelljs'); | ||
const { linkPackage } = require('./utils'); | ||
|
||
const root = join(__dirname, '../../../..'); | ||
|
||
async function linkPackage(name, from, to) { | ||
try { | ||
await symlink( | ||
join(from, 'node_modules', name), | ||
join(to, 'node_modules', name) | ||
); | ||
} catch {} | ||
} | ||
|
||
const argv = { | ||
_: [], | ||
src: 'src', | ||
dest: 'build', | ||
config: 'preact.config.js', | ||
prerenderUrls: 'prerender-urls.json', | ||
'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; | ||
}; | ||
|
||
exports.build = async function (cwd, options, installNodeModules = false) { | ||
const argv = { | ||
src: 'src', | ||
dest: 'build', | ||
config: 'preact.config.js', | ||
prerenderUrls: 'prerender-urls.json', | ||
'inline-css': true, | ||
}; | ||
|
||
if (!installNodeModules) { | ||
await mkdir(join(cwd, 'node_modules'), { recursive: true }); // ensure exists, avoid exit() | ||
await linkPackage('preact', root, cwd); | ||
await linkPackage('preact-render-to-string', root, cwd); | ||
await linkPackage('preact', cwd); | ||
await linkPackage('preact-render-to-string', cwd); | ||
} else { | ||
shell.cd(cwd); | ||
shell.exec('npm i'); | ||
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); | ||
}; | ||
|
||
exports.watch = function (cwd, port, host = '127.0.0.1') { | ||
const args = { ...argv }; | ||
delete args.dest; | ||
delete args['inline-css']; | ||
let opts = Object.assign({ cwd, host, port, https: false }, args); | ||
return cmd.watch(argv.src, opts); | ||
exports.watch = function (cwd, options) { | ||
const argv = { | ||
src: 'src', | ||
host: '127.0.0.1', | ||
https: false, | ||
config: 'preact.config.js', | ||
prerenderUrls: 'prerender-urls.json', | ||
}; | ||
|
||
let opts = Object.assign({ cwd }, argv, options); | ||
return cmd.watch(opts.src, opts); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const { join } = require('path'); | ||
const shell = require('shelljs'); | ||
|
||
describe('preact list', () => { | ||
it('lists the official templates', () => { | ||
const { code, stdout } = shell.exec( | ||
`node ${join(__dirname, '../src/index.js')} list` | ||
); | ||
[ | ||
'default', | ||
'netlify', | ||
'simple', | ||
'typescript', | ||
'widget', | ||
'widget-typescript', | ||
].forEach(repoName => { | ||
expect(stdout).toMatch(new RegExp(`${repoName}.* -`, 'g')); | ||
}); | ||
expect(code).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { h } from 'preact'; | ||
|
||
export default () => <h2>This is an app with custom template</h2>; | ||
export default () => <h2>This is an app with custom webpack config</h2>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
module.exports = function (config) { | ||
config.output.filename = '[name].js'; | ||
module.exports = function (config, env) { | ||
if (env.isProd) { | ||
config.output.filename = '[name].js'; | ||
} else { | ||
config.output.filename = 'renamed-bundle.js'; | ||
} | ||
}; |
Oops, something went wrong.