Skip to content

Commit 9ca7361

Browse files
committed
docs: ✏️ split test files into multiple files
1 parent b30950e commit 9ca7361

File tree

10 files changed

+363
-434
lines changed

10 files changed

+363
-434
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
*.log
2+
*.log
3+
coverage

Diff for: package.json

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
"prepublishOnly": "npm run format && npm run test",
2626
"postinstall": "node -e \"console.log('\\u001b[36m\\u001b[1m[svelte-preprocess] Don\\'t forget to install the preprocessors packages that will be used: \\u001b[22m\\u001b[39m\\u001b[34mnode-sass, stylus, less, postcss, coffesscript, pug, etc...\\u001b[0m')\""
2727
},
28+
"jest": {
29+
"collectCoverage": true,
30+
"collectCoverageFrom": [
31+
"<rootDir>/src/**/*.js",
32+
"!**/node_modules/**"
33+
]
34+
},
2835
"devDependencies": {
2936
"auto-changelog": "^1.11.0",
3037
"autoprefixer": "^9.4.7",

Diff for: src/transformers/stylus.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ module.exports = ({ content, filename, options }) => {
1818
style.render((err, css) => {
1919
if (err) reject(err)
2020

21-
resolve({
22-
code: css,
23-
map: style.sourcemap,
24-
})
21+
resolve({ code: css, map: style.sourcemap })
2522
})
2623
})
2724
}

Diff for: test/autoProcess/autoProcess.test.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const getAutoPreprocess = require('../../src')
2+
const { getLanguage } = require('../../src/utils.js')
3+
4+
const {
5+
preprocess,
6+
getFixtureContent,
7+
doesCompileThrow,
8+
CSS_PATTERN,
9+
} = require('../utils.js')
10+
11+
describe('detect - mimetype', () => {
12+
const MIMETYPES = [
13+
{ type: 'application/ld+json', parser: 'ld+json' },
14+
{ type: 'text/some-other', parser: 'some-other' },
15+
{ lang: 'stylus', parser: 'stylus' },
16+
{ src: '../foo.custom', lang: 'customLanguage', parser: 'customLanguage' },
17+
]
18+
19+
MIMETYPES.forEach(({ type, lang, src, parser }) => {
20+
it(`should detect '${src || type || lang}' as '${parser}'`, async () => {
21+
const language = getLanguage({ type, lang, src }, 'javascript')
22+
expect(language).toEqual({ lang: parser, alias: parser })
23+
})
24+
})
25+
})
26+
27+
describe('options', () => {
28+
it('should accept custom method for a transformer', async () => {
29+
const input = `<template lang="customTransformer">${getFixtureContent(
30+
'template.custom',
31+
)}</template>`
32+
const opts = getAutoPreprocess({
33+
transformers: {
34+
customTransformer({ content, filename }) {
35+
content = content
36+
.replace('foo', 'bar')
37+
.toString()
38+
.trim()
39+
return { code: content, map: null }
40+
},
41+
},
42+
})
43+
const preprocessed = await preprocess(input, opts)
44+
expect(preprocessed.toString()).toBe('bar')
45+
})
46+
47+
it('should accept an options object as transformer value', async () => {
48+
const input = `<div></div><style src="./fixtures/style.scss"></style>`
49+
const preprocessed = await preprocess(
50+
input,
51+
getAutoPreprocess({
52+
transformers: {
53+
scss: {
54+
sourceMap: false,
55+
includedPaths: ['node_modules'],
56+
},
57+
},
58+
}),
59+
)
60+
expect(preprocessed.toString()).toMatch(CSS_PATTERN)
61+
})
62+
63+
it('should execute a onBefore method before transforming markup', async () => {
64+
const input = `UPPERCASE?`
65+
const opts = getAutoPreprocess({
66+
async onBefore({ content }) {
67+
return content.toLowerCase()
68+
},
69+
})
70+
const preprocessed = await preprocess(input, opts)
71+
expect(preprocessed.toString()).toBe(input.toLowerCase())
72+
})
73+
74+
it('should append aliases to the language alias dictionary', async () => {
75+
const input = `<div></div><style lang="customLanguage"></style>`
76+
const opts = getAutoPreprocess({
77+
aliases: [['customLanguage', 'css']],
78+
})
79+
expect(await doesCompileThrow(input, opts)).toBe(false)
80+
})
81+
82+
it('should NOT preprocess preserved languages', async () => {
83+
const input = `<div></div><script type="application/ld+json">{"json":true}</script>`
84+
const opts = getAutoPreprocess({
85+
preserve: ['ld+json'],
86+
aliases: [['ld+json', 'structuredData']],
87+
transformers: {
88+
structuredData() {
89+
return { code: '', map: '' }
90+
},
91+
},
92+
})
93+
const preprocessed = await preprocess(input, opts)
94+
expect(preprocessed.toString()).toContain(
95+
`<script type="application/ld+json">{"json":true}</script>`,
96+
)
97+
})
98+
99+
it('should allow to pass specific options to alias', async () => {
100+
const input = `<div></div><style lang="sass">${getFixtureContent(
101+
'style.sass',
102+
)}</style>`
103+
104+
const opts = getAutoPreprocess({
105+
transformers: {
106+
sass: {
107+
indentedSyntax: false,
108+
},
109+
},
110+
})
111+
112+
expect(await doesCompileThrow(input, opts)).toBe(true)
113+
})
114+
})

Diff for: test/autoProcess/externalFiles.test.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const { resolve } = require('path')
2+
3+
const getAutoPreprocess = require('../../src')
4+
const { preprocess, getFixtureContent, getFixturePath } = require('../utils.js')
5+
6+
const {
7+
markup: markupProcessor,
8+
script: scriptProcessor,
9+
style: styleProcessor,
10+
} = getAutoPreprocess()
11+
12+
const getBaseObj = src => ({
13+
content: `<template src="./fixtures/template.html"></template>
14+
<style src="./fixtures/style.css"></style>
15+
<script src="./fixtures/script.js"></script>`,
16+
filename: resolve(__dirname, '..', 'App.svelte'),
17+
attributes: { src: `./fixtures/${src}` },
18+
})
19+
20+
let markup, script, style
21+
22+
describe('external files', () => {
23+
beforeAll(async () => {
24+
;[markup, script, style] = [
25+
await markupProcessor(getBaseObj('template.html')),
26+
await scriptProcessor(getBaseObj('script.js')),
27+
await styleProcessor(getBaseObj('style.css')),
28+
]
29+
})
30+
31+
it('should insert external file content', async () => {
32+
expect(markup.code).toContain(getFixtureContent('template.html'))
33+
expect(script.code).toContain(getFixtureContent('script.js'))
34+
expect(style.code).toContain(getFixtureContent('style.css'))
35+
})
36+
37+
it('should add a external file as a dependency', async () => {
38+
expect(markup.dependencies).toContain(getFixturePath('template.html'))
39+
expect(script.dependencies).toContain(getFixturePath('script.js'))
40+
expect(style.dependencies).toContain(getFixturePath('style.css'))
41+
})
42+
43+
const EXTERNALJS = [
44+
'https://www.example.com/some/externally/delivered/content.js',
45+
'http://www.example.com/some/externally/delivered/content.js',
46+
'//www.example.com/some/externally/delivered/content.js',
47+
]
48+
49+
EXTERNALJS.forEach(url => {
50+
it(`should not attempt to locally resolve ${url}`, async () => {
51+
const input = `<div></div><script src="${url}"></script>`
52+
53+
const preprocessed = await preprocess(input, getAutoPreprocess())
54+
expect(preprocessed.toString()).toContain(input)
55+
expect(preprocessed.dependencies.length).toBe(0)
56+
})
57+
})
58+
})

Diff for: test/autoProcess/markup.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const getAutoPreprocess = require('../../src')
2+
const {
3+
preprocess,
4+
getFixtureContent,
5+
doesCompileThrow,
6+
} = require('../utils.js')
7+
8+
const EXPECTED_MARKUP = getFixtureContent('template.html')
9+
const MARKUP_LANGS = [['pug', 'pug']]
10+
11+
it('should parse HTML between <template></template>', async () => {
12+
const input = `<template><div>Hey</div></template>`
13+
const preprocessed = await preprocess(input, getAutoPreprocess())
14+
expect(preprocessed.toString()).toBe(EXPECTED_MARKUP)
15+
})
16+
17+
MARKUP_LANGS.forEach(([lang, ext]) => {
18+
describe(`markup - preprocessor - ${lang}`, () => {
19+
const template = `<template lang="${lang}">${getFixtureContent(
20+
`template.${ext}`,
21+
)}</template>`
22+
23+
it(`should throw parsing ${lang} when { ${lang}: false }`, async () => {
24+
const opts = getAutoPreprocess({ transformers: { pug: false } })
25+
expect(await doesCompileThrow(template, opts)).toBe(true)
26+
})
27+
28+
it(`should parse ${lang}`, async () => {
29+
const preprocessed = (await preprocess(template, getAutoPreprocess()))
30+
.toString()
31+
.trim()
32+
expect(preprocessed.toString()).toBe(EXPECTED_MARKUP)
33+
})
34+
})
35+
})

Diff for: test/autoProcess/script.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const getAutoPreprocess = require('../../src')
2+
const {
3+
preprocess,
4+
getFixtureContent,
5+
doesCompileThrow,
6+
} = require('../utils.js')
7+
8+
const SCRIPT_LANGS = [['coffeescript', 'coffee']]
9+
const EXPECTED_SCRIPT = getFixtureContent('script.js')
10+
11+
SCRIPT_LANGS.forEach(([lang, ext, langOptions]) => {
12+
describe(`script - preprocessor - ${lang}`, () => {
13+
const template = `<div></div><script lang="${lang}">${getFixtureContent(
14+
'script.' + ext,
15+
)}</script>`
16+
17+
it(`should throw parsing ${lang} when { ${lang}: false }`, async () => {
18+
const input = `
19+
<div></div>
20+
<script src="./fixtures/script.${ext}"></script>
21+
`
22+
const opts = getAutoPreprocess({
23+
transformers: {
24+
[lang]: false,
25+
},
26+
})
27+
expect(await doesCompileThrow(input, opts)).toBe(true)
28+
})
29+
30+
it(`should parse ${lang}`, async () => {
31+
const opts = getAutoPreprocess()
32+
const preprocessed = await preprocess(template, opts)
33+
expect(preprocessed.toString()).toContain(EXPECTED_SCRIPT)
34+
})
35+
})
36+
})

Diff for: test/autoProcess/style.test.js

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
const getAutoPreprocess = require('../../src')
2+
const {
3+
preprocess,
4+
getFixtureContent,
5+
doesCompileThrow,
6+
CSS_PATTERN,
7+
} = require('../utils.js')
8+
9+
const STYLE_LANGS = [
10+
['sass', 'sass'],
11+
['less', 'less'],
12+
['scss', 'scss'],
13+
['stylus', 'styl'],
14+
]
15+
16+
STYLE_LANGS.forEach(([lang, ext, langOptions]) => {
17+
describe(`style - preprocessor - ${lang}`, () => {
18+
const template = `<div></div><style lang="${lang}">${getFixtureContent(
19+
'style.' + ext,
20+
)}</style>`
21+
const templateExternal = `<div></div><style src="./fixtures/style.${ext}"></style>`
22+
23+
it(`should throw parsing ${lang} when { ${lang}: false }`, async () => {
24+
const opts = getAutoPreprocess({
25+
transformers: {
26+
[lang]: false,
27+
},
28+
})
29+
expect(await doesCompileThrow(template, opts)).toBe(true)
30+
})
31+
32+
it(`should parse ${lang}`, async () => {
33+
const opts = getAutoPreprocess()
34+
const preprocessed = await preprocess(template, opts)
35+
expect(preprocessed.toString()).toMatch(CSS_PATTERN)
36+
})
37+
38+
it(`should parse external ${lang}`, async () => {
39+
const opts = getAutoPreprocess()
40+
const preprocessed = await preprocess(templateExternal, opts)
41+
expect(preprocessed.toString()).toMatch(CSS_PATTERN)
42+
})
43+
})
44+
})
45+
46+
describe('style - postcss', () => {
47+
const template = `<div></div><style>div{appearance:none;}</style>`
48+
const templateSass = `<div></div><style lang="scss">div{appearance:none;}</style>`
49+
const optsWithoutConfigFile = getAutoPreprocess({
50+
transformers: {
51+
postcss: {
52+
plugins: [
53+
require('autoprefixer')({
54+
browsers: 'Safari >= 5.1',
55+
}),
56+
],
57+
},
58+
},
59+
})
60+
61+
it('should not transform plain css with postcss if { postcss: falsy }', async () => {
62+
const preprocessed = await preprocess(template, getAutoPreprocess())
63+
expect(preprocessed.toString()).not.toMatch(/-webkit-/)
64+
})
65+
66+
it('should not transform plain css with postcss if { postcss: true } and no configuration file at cwd', async () => {
67+
const preprocessed = await preprocess(
68+
template,
69+
getAutoPreprocess({
70+
transformers: {
71+
postcss: true,
72+
},
73+
}),
74+
)
75+
expect(preprocessed.toString()).not.toMatch(/-webkit-/)
76+
})
77+
78+
it('should transform plain css with postcss if { postcss: { plugins... } }', async () => {
79+
const preprocessed = await preprocess(template, optsWithoutConfigFile)
80+
expect(preprocessed.toString()).toMatch(/-webkit-/)
81+
})
82+
83+
it('should transform async preprocessed css with postcss if { postcss: { plugins... } }', async () => {
84+
const preprocessed = await preprocess(templateSass, optsWithoutConfigFile)
85+
expect(preprocessed.toString()).toMatch(/-webkit-/)
86+
})
87+
88+
it('should transform plain css with postcss if { postcss: { configFilePath: ... } }', async () => {
89+
const preprocessed = await preprocess(
90+
template,
91+
getAutoPreprocess({
92+
transformers: {
93+
postcss: {
94+
configFilePath: './test/fixtures/',
95+
},
96+
},
97+
}),
98+
)
99+
expect(preprocessed.toString()).toMatch(/-webkit-/)
100+
})
101+
})

0 commit comments

Comments
 (0)