From 788aa89b41380f13988a59e6dd6ccf4e3c4e26b9 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 1 Apr 2018 14:00:35 -0400 Subject: [PATCH] add stats tests, handle imports --- src/Stats.ts | 24 ++++++++++- src/generators/Generator.ts | 4 +- test/stats/index.js | 59 +++++++++++++++++++++++++++ test/stats/samples/basic/_config.js | 6 +++ test/stats/samples/basic/input.html | 0 test/stats/samples/imports/_config.js | 18 ++++++++ test/stats/samples/imports/input.html | 5 +++ 7 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 test/stats/index.js create mode 100644 test/stats/samples/basic/_config.js create mode 100644 test/stats/samples/basic/input.html create mode 100644 test/stats/samples/imports/_config.js create mode 100644 test/stats/samples/imports/input.html diff --git a/src/Stats.ts b/src/Stats.ts index fd2c45ce0a..7994d9b048 100644 --- a/src/Stats.ts +++ b/src/Stats.ts @@ -1,3 +1,5 @@ +import { Node } from './interfaces'; + const now = (typeof process !== 'undefined' && process.hrtime) ? () => { const t = process.hrtime(); @@ -61,13 +63,31 @@ export default class Stats { this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings; } - toJSON() { + render({ imports }: { + imports: Node[] + }) { const timings = Object.assign({ total: now() - this.startTime }, collapseTimings(this.timings)); return { - timings + timings, + warnings: [], // TODO + imports: imports.map(node => { + return { + source: node.source.value, + specifiers: node.specifiers.map(specifier => { + return { + name: ( + specifier.type === 'ImportDefaultSpecifier' ? 'default' : + specifier.type === 'ImportNamespaceSpecifier' ? '*' : + specifier.imported.name + ), + as: specifier.local.name + }; + }) + } + }) }; } } \ No newline at end of file diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 9a0e9acd43..cf423739d4 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -385,7 +385,9 @@ export default class Generator { ast: this.ast, js, css, - stats: this.stats.toJSON(), + stats: this.stats.render({ + imports: this.imports + }), // TODO deprecate code: js.code, diff --git a/test/stats/index.js b/test/stats/index.js new file mode 100644 index 0000000000..7ff64f0fe0 --- /dev/null +++ b/test/stats/index.js @@ -0,0 +1,59 @@ +import * as fs from 'fs'; +import assert from 'assert'; +import { svelte, loadConfig, tryToLoadJson } from '../helpers.js'; + +describe('stats', () => { + fs.readdirSync('test/stats/samples').forEach(dir => { + if (dir[0] === '.') return; + + // add .solo to a sample directory name to only run that test + const solo = /\.solo/.test(dir); + const skip = /\.skip/.test(dir); + + if (solo && process.env.CI) { + throw new Error('Forgot to remove `solo: true` from test'); + } + + (solo ? it.only : skip ? it.skip : it)(dir, () => { + const config = loadConfig(`./stats/samples/${dir}/_config.js`); + const filename = `test/stats/samples/${dir}/input.html`; + const input = fs.readFileSync(filename, 'utf-8').replace(/\s+$/, ''); + + const expectedWarnings = + tryToLoadJson(`test/stats/samples/${dir}/warnings.json`) || []; + const expectedError = tryToLoadJson( + `test/stats/samples/${dir}/error.json` + ); + + let result; + let error; + + try { + result = svelte.compile(input, config.options); + } catch (e) { + error = e; + } + + config.test(assert, result.stats); + + if (result.stats.warnings.length || expectedWarnings.length) { + // TODO check warnings are added to stats.warnings + } + + if (error || expectedError) { + if (error && !expectedError) { + throw error; + } + + if (expectedError && !error) { + throw new Error(`Expected an error: ${expectedError.message}`); + } + + assert.equal(error.message, expectedError.message); + assert.deepEqual(error.loc, expectedError.loc); + assert.deepEqual(error.end, expectedError.end); + assert.equal(error.pos, expectedError.pos); + } + }); + }); +}); diff --git a/test/stats/samples/basic/_config.js b/test/stats/samples/basic/_config.js new file mode 100644 index 0000000000..434e11bd76 --- /dev/null +++ b/test/stats/samples/basic/_config.js @@ -0,0 +1,6 @@ +export default { + test(assert, stats) { + assert.equal(typeof stats.timings, 'object'); + assert.equal(typeof stats.timings.total, 'number'); + } +}; \ No newline at end of file diff --git a/test/stats/samples/basic/input.html b/test/stats/samples/basic/input.html new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/stats/samples/imports/_config.js b/test/stats/samples/imports/_config.js new file mode 100644 index 0000000000..f7c15183dd --- /dev/null +++ b/test/stats/samples/imports/_config.js @@ -0,0 +1,18 @@ +export default { + test(assert, stats) { + assert.deepEqual(stats.imports, [ + { + source: 'x', + specifiers: [{ name: 'default', as: 'x' }] + }, + { + source: 'y', + specifiers: [{ name: 'y', as: 'y' }] + }, + { + source: 'z', + specifiers: [{ name: '*', as: 'z' }] + } + ]); + } +}; \ No newline at end of file diff --git a/test/stats/samples/imports/input.html b/test/stats/samples/imports/input.html new file mode 100644 index 0000000000..a7b59691ac --- /dev/null +++ b/test/stats/samples/imports/input.html @@ -0,0 +1,5 @@ + \ No newline at end of file