Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
restructure tests
* split up tests into separate files * fix coverage generation for SSR
- Loading branch information
Showing
13 changed files
with
589 additions
and
557 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 |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
--require reify | ||
--recursive | ||
./**/__test__.js | ||
test/test.js | ||
test/*.js |
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,4 +1,4 @@ | ||
--require reify | ||
--recursive | ||
./**/__test__.js | ||
test/test.js | ||
test/*.js |
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,178 @@ | ||
import deindent from '../src/utils/deindent.js'; | ||
import assert from 'assert'; | ||
import { svelte, env, setupHtmlEqual } from './helpers.js'; | ||
|
||
function testAmd ( code, expectedId, dependencies, html ) { | ||
const fn = new Function( 'define', code ); | ||
|
||
return env().then( window => { | ||
function define ( id, deps, factory ) { | ||
assert.equal( id, expectedId ); | ||
assert.deepEqual( deps, Object.keys( dependencies ) ); | ||
|
||
const SvelteComponent = factory( ...Object.keys( dependencies ).map( key => dependencies[ key ] ) ); | ||
|
||
const main = window.document.body.querySelector( 'main' ); | ||
const component = new SvelteComponent({ target: main }); | ||
|
||
assert.htmlEqual( main.innerHTML, html ); | ||
|
||
component.teardown(); | ||
} | ||
|
||
define.amd = true; | ||
|
||
fn( define ); | ||
}); | ||
} | ||
|
||
function testCjs ( code, dependencyById, html ) { | ||
const fn = new Function( 'module', 'exports', 'require', code ); | ||
|
||
return env().then( window => { | ||
const module = { exports: {} }; | ||
const require = id => { | ||
return dependencyById[ id ]; | ||
}; | ||
|
||
fn( module, module.exports, require ); | ||
|
||
const SvelteComponent = module.exports; | ||
|
||
const main = window.document.body.querySelector( 'main' ); | ||
const component = new SvelteComponent({ target: main }); | ||
|
||
assert.htmlEqual( main.innerHTML, html ); | ||
|
||
component.teardown(); | ||
}); | ||
} | ||
|
||
function testIife ( code, name, globals, html ) { | ||
const fn = new Function( Object.keys( globals ), `${code}\n\nreturn ${name};` ); | ||
|
||
return env().then( window => { | ||
const SvelteComponent = fn( ...Object.keys( globals ).map( key => globals[ key ] ) ); | ||
|
||
const main = window.document.body.querySelector( 'main' ); | ||
const component = new SvelteComponent({ target: main }); | ||
|
||
assert.htmlEqual( main.innerHTML, html ); | ||
|
||
component.teardown(); | ||
}); | ||
} | ||
|
||
describe( 'formats', () => { | ||
before( setupHtmlEqual ); | ||
|
||
describe( 'amd', () => { | ||
it( 'generates an AMD module', () => { | ||
const source = deindent` | ||
<div>{{answer}}</div> | ||
<script> | ||
import answer from 'answer'; | ||
export default { | ||
data () { | ||
return { answer }; | ||
} | ||
}; | ||
</script> | ||
`; | ||
|
||
const { code } = svelte.compile( source, { | ||
format: 'amd', | ||
amd: { id: 'foo' } | ||
}); | ||
|
||
return testAmd( code, 'foo', { answer: 42 }, `<div>42</div>` ); | ||
}); | ||
}); | ||
|
||
describe( 'cjs', () => { | ||
it( 'generates a CommonJS module', () => { | ||
const source = deindent` | ||
<div>{{answer}}</div> | ||
<script> | ||
import answer from 'answer'; | ||
export default { | ||
data () { | ||
return { answer }; | ||
} | ||
}; | ||
</script> | ||
`; | ||
|
||
const { code } = svelte.compile( source, { | ||
format: 'cjs' | ||
}); | ||
|
||
return testCjs( code, { answer: 42 }, `<div>42</div>` ); | ||
}); | ||
}); | ||
|
||
describe( 'iife', () => { | ||
it( 'generates a self-executing script', () => { | ||
const source = deindent` | ||
<div>{{answer}}</div> | ||
<script> | ||
import answer from 'answer'; | ||
export default { | ||
data () { | ||
return { answer }; | ||
} | ||
}; | ||
</script> | ||
`; | ||
|
||
const { code } = svelte.compile( source, { | ||
format: 'iife', | ||
name: 'Foo', | ||
globals: { | ||
answer: 'answer' | ||
} | ||
}); | ||
|
||
return testIife( code, 'Foo', { answer: 42 }, `<div>42</div>` ); | ||
}); | ||
}); | ||
|
||
describe( 'umd', () => { | ||
it( 'generates a UMD build', () => { | ||
const source = deindent` | ||
<div>{{answer}}</div> | ||
<script> | ||
import answer from 'answer'; | ||
export default { | ||
data () { | ||
return { answer }; | ||
} | ||
}; | ||
</script> | ||
`; | ||
|
||
const { code } = svelte.compile( source, { | ||
format: 'umd', | ||
name: 'Foo', | ||
globals: { | ||
answer: 'answer' | ||
}, | ||
amd: { | ||
id: 'foo' | ||
} | ||
}); | ||
|
||
return testAmd( code, 'foo', { answer: 42 }, `<div>42</div>` ) | ||
.then( () => testCjs( code, { answer: 42 }, `<div>42</div>` ) ) | ||
.then( () => testIife( code, 'Foo', { answer: 42 }, `<div>42</div>` ) ); | ||
}); | ||
}); | ||
}); |
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,121 @@ | ||
import spaces from '../src/utils/spaces.js'; | ||
import assert from 'assert'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import * as acorn from 'acorn'; | ||
|
||
import { svelte, env, setupHtmlEqual } from './helpers.js'; | ||
|
||
const cache = {}; | ||
|
||
let showCompiledCode = false; | ||
let compileOptions = null; | ||
|
||
require.extensions[ '.html' ] = function ( module, filename ) { | ||
const options = Object.assign({ filename }, compileOptions ); | ||
const code = cache[ filename ] || ( cache[ filename ] = svelte.compile( fs.readFileSync( filename, 'utf-8' ), options ).code ); | ||
if ( showCompiledCode ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console | ||
|
||
return module._compile( code, filename ); | ||
}; | ||
|
||
function addLineNumbers ( code ) { | ||
return code.split( '\n' ).map( ( line, i ) => { | ||
i = String( i + 1 ); | ||
while ( i.length < 3 ) i = ` ${i}`; | ||
|
||
return `${i}: ${line.replace( /^\t+/, match => match.split( '\t' ).join( ' ' ) )}`; | ||
}).join( '\n' ); | ||
} | ||
|
||
function loadConfig ( dir ) { | ||
try { | ||
return require( `./generator/${dir}/_config.js` ).default; | ||
} catch ( err ) { | ||
if ( err.code === 'E_NOT_FOUND' ) { | ||
return {}; | ||
} | ||
|
||
throw err; | ||
} | ||
} | ||
|
||
describe( 'generate', () => { | ||
before( setupHtmlEqual ); | ||
|
||
fs.readdirSync( 'test/generator' ).forEach( dir => { | ||
if ( dir[0] === '.' ) return; | ||
|
||
const config = loadConfig( dir ); | ||
|
||
( config.skip ? it.skip : config.solo ? it.only : it )( dir, () => { | ||
let compiled; | ||
|
||
showCompiledCode = config.show; | ||
compileOptions = config.compileOptions || {}; | ||
|
||
try { | ||
const source = fs.readFileSync( `test/generator/${dir}/main.html`, 'utf-8' ); | ||
compiled = svelte.compile( source ); | ||
} catch ( err ) { | ||
if ( config.compileError ) { | ||
config.compileError( err ); | ||
return; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
|
||
const { code } = compiled; | ||
|
||
// check that no ES2015+ syntax slipped in | ||
try { | ||
const startIndex = code.indexOf( 'function renderMainFragment' ); // may change! | ||
const es5 = spaces( startIndex ) + code.slice( startIndex ).replace( /export default .+/, '' ); | ||
acorn.parse( es5, { ecmaVersion: 5 }); | ||
} catch ( err ) { | ||
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console | ||
throw err; | ||
} | ||
|
||
cache[ path.resolve( `test/generator/${dir}/main.html` ) ] = code; | ||
|
||
let SvelteComponent; | ||
|
||
try { | ||
SvelteComponent = require( `./generator/${dir}/main.html` ).default; | ||
} catch ( err ) { | ||
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console | ||
throw err; | ||
} | ||
|
||
return env() | ||
.then( window => { | ||
// Put the constructor on window for testing | ||
window.SvelteComponent = SvelteComponent; | ||
|
||
const target = window.document.querySelector( 'main' ); | ||
|
||
const component = new SvelteComponent({ | ||
target, | ||
data: config.data | ||
}); | ||
|
||
if ( config.html ) { | ||
assert.htmlEqual( target.innerHTML, config.html ); | ||
} | ||
|
||
if ( config.test ) { | ||
config.test( assert, component, target, window ); | ||
} else { | ||
component.teardown(); | ||
assert.equal( target.innerHTML, '' ); | ||
} | ||
}) | ||
.catch( err => { | ||
if ( !config.show ) console.log( addLineNumbers( code ) ); // eslint-disable-line no-console | ||
throw err; | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.