Skip to content

Commit 2c7bc30

Browse files
committed
test: 💍 add some more coverge
1 parent 65e3b56 commit 2c7bc30

15 files changed

+158
-19
lines changed

Diff for: src/modules/errors.ts

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ export const throwError = (msg: string) => {
22
throw new Error(`[svelte-preprocess] ${msg}`);
33
};
44

5-
export const throwUnsupportedError = (lang: string, filename: string) =>
6-
throwError(`Unsupported script language '${lang}' in file '${filename}'`);
7-
85
export const throwTypescriptError = () => {
96
throwError(`Encountered type error`);
107
};

Diff for: src/modules/prepareContent.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function prepareContent({
1313

1414
content = stripIndent(content);
1515

16-
if (options?.prependData) {
16+
if (options.prependData) {
1717
content = `${options.prependData}\n${content}`;
1818
}
1919

Diff for: src/transformers/babel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const transformer: Transformer<Options.Babel> = async ({
88
options,
99
map = undefined,
1010
}) => {
11-
if (!options) {
11+
if (!options || typeof options !== 'object') {
1212
return { code: content, map };
1313
}
1414

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { resolve } from 'path';
22

33
import getAutoPreprocess from '../../src';
44
import { Processed } from '../../src/types';
5-
import { preprocess, getFixtureContent, getFixturePath } from '../utils';
5+
import {
6+
preprocess,
7+
getFixtureContent,
8+
getFixturePath,
9+
spyConsole,
10+
} from '../utils';
11+
12+
const { warnSpy } = spyConsole();
613

714
const {
815
markup: markupProcessor,
@@ -66,13 +73,12 @@ describe('external files', () => {
6673
});
6774

6875
it("should warn if local file don't exist", async () => {
69-
const spy = jest.spyOn(console, 'warn');
7076
const input = `<style src="./missing-potato"></style>`;
7177

7278
await preprocess(input, getAutoPreprocess());
7379

74-
expect(spy).toHaveBeenCalledWith(expect.stringContaining('was not found'));
75-
76-
spy.mockRestore();
80+
expect(warnSpy).toHaveBeenCalledWith(
81+
expect.stringContaining('was not found'),
82+
);
7783
});
7884
});

Diff for: test/modules.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getTagInfo } from '../src/modules/tagInfo';
55
import { importAny } from '../src/modules/importAny';
66
import { getIncludePaths } from '../src/modules/getIncludePaths';
77
import { globalifySelector } from '../src/modules/globalifySelector';
8+
import { hasDepInstalled } from '../src/modules/hasDepInstalled';
89

910
describe('importAny', () => {
1011
it('should throw error when none exist', () => {
@@ -99,3 +100,13 @@ describe(`get tag information`, () => {
99100
expect(parsedFile.content).toEqual(getFixtureContent('style.scss'));
100101
});
101102
});
103+
104+
describe('has dependency installed', () => {
105+
it('should return true if dependency is installed', async () => {
106+
expect(await hasDepInstalled('svelte')).toBe(true);
107+
});
108+
109+
it('should return false if dependency is installed', async () => {
110+
expect(await hasDepInstalled('potatonator')).toBe(false);
111+
});
112+
});

Diff for: test/processors/coffeescript.test.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { coffeescript } from '../../src';
2+
import { getFixtureContent, preprocess } from '../utils';
3+
4+
const EXPECTED_SCRIPT = getFixtureContent('script.js');
5+
6+
describe(`processor - coffeescript`, () => {
7+
it('should ignore other languages', async () => {
8+
const template = `<script lang="potato">🥔</script>`;
9+
const options = {};
10+
11+
const preprocessed = await preprocess(template, [coffeescript(options)]);
12+
13+
expect(preprocessed.toString()).toBe(template);
14+
});
15+
16+
it('should support external src files', async () => {
17+
const template = `<script src="./fixtures/script.coffee"></script>`;
18+
const options = {};
19+
20+
const preprocessed = await preprocess(template, [coffeescript(options)]);
21+
22+
expect(preprocessed.toString()).toContain(EXPECTED_SCRIPT);
23+
});
24+
25+
it('should support prepended data', async () => {
26+
const template = `<script src="./fixtures/script.coffee"></script>`;
27+
const options = {
28+
prependData: '### potato ###',
29+
};
30+
31+
const preprocessed = await preprocess(template, [coffeescript(options)]);
32+
33+
expect(preprocessed.toString()).toContain('/* potato */');
34+
});
35+
});

Diff for: test/processors/less.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { less } from '../../src';
22
import { CSS_PATTERN, preprocess } from '../utils';
33

44
describe(`processor - less`, () => {
5+
it('should ignore other languages', async () => {
6+
const template = `<style lang="potato">🥔</style>`;
7+
const options = {};
8+
9+
const preprocessed = await preprocess(template, [less(options)]);
10+
11+
expect(preprocessed.toString()).toBe(template);
12+
});
13+
514
it('should support external src files', async () => {
615
const template = `<style src="./fixtures/style.less"></style><div></div>`;
716
const preprocessed = await preprocess(template, [less()]);

Diff for: test/processors/postcss.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { postcss } from '../../src';
2-
import { CSS_PATTERN, preprocess } from '../utils';
2+
import { CSS_PATTERN, preprocess, spyConsole } from '../utils';
3+
4+
spyConsole();
35

46
describe(`processor - postcss`, () => {
57
it('should support external src files', async () => {

Diff for: test/processors/scss.test.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { scss, sass } from '../../src';
22
import { CSS_PATTERN, preprocess } from '../utils';
33

44
describe(`processor - sass`, () => {
5+
it('should ignore other languages', async () => {
6+
const template = `<style lang="potato">🥔</style>`;
7+
const options = {};
8+
9+
const preprocessed = await preprocess(template, [sass(options)]);
10+
11+
expect(preprocessed.toString()).toBe(template);
12+
});
13+
514
it('should support external src files', async () => {
615
const template = `<style src="./fixtures/style.sass"></style><div></div>`;
716
const preprocessed = await preprocess(template, [sass()]);
@@ -10,15 +19,24 @@ describe(`processor - sass`, () => {
1019
});
1120

1221
it('should support prepended data', async () => {
13-
const template = `<style src="./fixtures/stysassxt}"></style><div></div>`;
14-
const options = { prependData: '// potato' };
22+
const template = `<style src="./fixtures/style.sass"></style><div></div>`;
23+
const options = { prependData: '/* potato */' };
1524
const preprocessed = await preprocess(template, [sass(options)]);
1625

17-
expect(preprocessed.toString()).toContain('// potato');
26+
expect(preprocessed.toString()).toContain('/* potato');
1827
});
1928
});
2029

2130
describe(`processor - scss`, () => {
31+
it('should ignore other languages', async () => {
32+
const template = `<script lang="potato">🥔</script>`;
33+
const options = {};
34+
35+
const preprocessed = await preprocess(template, [scss(options)]);
36+
37+
expect(preprocessed.toString()).toBe(template);
38+
});
39+
2240
it('should support external src files', async () => {
2341
const template = `<style src="./fixtures/style.scss"></style><div></div>`;
2442
const preprocessed = await preprocess(template, [scss()]);

Diff for: test/processors/stylus.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { stylus } from '../../src';
22
import { CSS_PATTERN, preprocess } from '../utils';
33

44
describe(`processor - stylus`, () => {
5+
it('should ignore other languages', async () => {
6+
const template = `<style lang="potato">🥔</style>`;
7+
const options = {};
8+
9+
const preprocessed = await preprocess(template, [stylus(options)]);
10+
11+
expect(preprocessed.toString()).toBe(template);
12+
});
13+
514
it('should support external src files', async () => {
615
const template = `<style src="./fixtures/style.styl"></style><div></div>`;
716
const preprocessed = await preprocess(template, [stylus()]);

Diff for: test/processors/typescript.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import { getFixtureContent, preprocess } from '../utils';
44
const EXPECTED_SCRIPT = getFixtureContent('script.js');
55

66
describe(`processor - typescript`, () => {
7+
it('should ignore other languages', async () => {
8+
const template = `<script lang="potato">🥔</script>`;
9+
const options = {};
10+
11+
const preprocessed = await preprocess(template, [typescript(options)]);
12+
13+
expect(preprocessed.toString()).toBe(template);
14+
});
15+
716
it('should support external src files', async () => {
817
const template = `<script src="./fixtures/script.ts"></script><div></div>`;
918
const options = {

Diff for: test/transformers/babel.test.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@ const BABEL_CONFIG = {
1717
};
1818

1919
describe('transformer - babel', () => {
20+
it("doesn't do anything without options", async () => {
21+
const template = `<script>
22+
let foo = {}
23+
$: bar = foo?.b ?? 120
24+
</script>`;
25+
26+
const opts = getAutoPreprocess({ babel: true });
27+
28+
const preprocessed = await preprocess(template, opts);
29+
30+
expect(preprocessed.code).toMatch(template);
31+
});
32+
2033
it('transpiles with babel', async () => {
2134
const template = `<script>
22-
let foo = {}
23-
$: bar = foo?.b ?? 120
24-
</script>`;
35+
let foo = {}
36+
$: bar = foo?.b ?? 120
37+
</script>`;
2538

2639
const opts = getAutoPreprocess({
2740
babel: BABEL_CONFIG,

Diff for: test/transformers/postcss.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import { resolve } from 'path';
55

66
import getAutoPreprocess from '../../src';
7-
import { preprocess } from '../utils';
7+
import { preprocess, spyConsole } from '../utils';
8+
9+
spyConsole();
810

911
describe('transformer - postcss', () => {
1012
it('should not transform plain css with postcss if { postcss: falsy }', async () => {

Diff for: test/transformers/typescript.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Diagnostic } from 'typescript';
44

55
import getAutoPreprocess from '../../src';
66
import { Processed } from '../../src/types';
7-
import { preprocess, getFixtureContent } from '../utils';
7+
import { preprocess, getFixtureContent, spyConsole } from '../utils';
8+
9+
spyConsole();
810

911
const EXPECTED_SCRIPT = getFixtureContent('script.js');
1012

Diff for: test/utils.ts

+26
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,29 @@ export const getFixturePath = (file: string) =>
3939

4040
export const getFixtureContent = (file: string) =>
4141
readFileSync(exports.getFixturePath(file)).toString().trim();
42+
43+
export function spyConsole({ silent = true } = {}) {
44+
const warnSpy = jest.spyOn(global.console, 'warn');
45+
const errorSpy = jest.spyOn(global.console, 'error');
46+
const logSpy = jest.spyOn(global.console, 'log');
47+
48+
if (silent) {
49+
warnSpy.mockImplementation(() => {});
50+
errorSpy.mockImplementation(() => {});
51+
logSpy.mockImplementation(() => {});
52+
}
53+
54+
afterAll(() => {
55+
warnSpy.mockRestore();
56+
errorSpy.mockRestore();
57+
logSpy.mockRestore();
58+
});
59+
60+
afterEach(() => {
61+
warnSpy.mockClear();
62+
errorSpy.mockClear();
63+
logSpy.mockClear();
64+
});
65+
66+
return { warnSpy, errorSpy, logSpy };
67+
}

0 commit comments

Comments
 (0)