Skip to content

Commit

Permalink
issue-132 - add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Jun 28, 2024
1 parent c581ccc commit 634f13b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
],
"require": [
"ts-node/register"
]
],
"snapshotDir": "./test/snapshots"
},
"dependencies": {
"@rollup/pluginutils": "^3 || ^4 || ^5",
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/multiple-entry-points/entryA.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import a from '../../assets/actual_a.scss';

export default a;
3 changes: 3 additions & 0 deletions test/fixtures/multiple-entry-points/entryB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import b from '../../assets/actual_b.scss';

export default b;
64 changes: 58 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import test from 'ava';
import {promises as fs, constants as fsConstants} from 'fs';
import * as path from 'path';
import test from 'ava';
import sinon from 'sinon';
import {OutputOptions, rollup, RollupOutput} from 'rollup';
import * as sassJs from 'sass';
import sass from '../src/index';
import {SassOptions} from "../src/types";
import {error} from "../src/utils";
import postcss from "postcss";
import {extractICSS} from "icss-utils";

import sass from "../src/index";
import { SassOptions } from "../src/types";

const repoRoot = path.join(__dirname, '../'),

tmpDir = path.join(repoRoot, '.tests-output/'),
Expand Down Expand Up @@ -86,6 +86,22 @@ test('should import *.scss and *.sass files', async t => {
t.true([expectA, expectB, expectC].every(xs => rslt.includes(xs)));
});

test("should import *.scss and *.sass files with default configuration", async (t) => {
const outputBundle = await rollup({
input: "test/fixtures/basic/index.js",
plugins: [
sass(),
],
}),
{ output } = await outputBundle.generate({
format: "es",
file: path.join(tmpDir, "import_scss_and_sass_default_options.js"),
}),
rslt = squash(unwrap(output));

t.snapshot(rslt)
});

test('should compress the dest CSS', async t => {
const outputBundle = await rollup({
...baseConfig,
Expand Down Expand Up @@ -152,8 +168,44 @@ test('should insert CSS into head tag', async t => {
}),
{output} = await outputBundle.generate(generateOptions);

t.true(unwrap(output).includes('___$insertStyle("body{color:red}");'));
t.true(unwrap(output).includes('___$insertStyle("body{color:green}");'));
t.snapshot(unwrap(output));
});

test("should generate chunks with import insertStyle when `insert` is true", async (t) => {
const outputBundle = await rollup({
input: {
entryA: "test/fixtures/multiple-entry-points/entryA.js",
entryB: "test/fixtures/multiple-entry-points/entryB.js",
},
plugins: [
sass({
insert: true,
options: sassOptions,
}),
],
output: {
preserveModules: true,
preserveModulesRoot: "src",
},
external: [/\/insertStyle\.js$/],
});

const { output } = await outputBundle.generate(generateOptions);

t.is(output.length, 2, "has 2 chunks");
t.true(
output.every(
(chunk) =>
chunk.type === "chunk" &&
chunk.imports.some((it) => it.includes("/insertStyle.js"))
),
"each chunk must include insertStyle"
);

// outputBundle.write({
// format: 'es',
// dir: path.join(tmpDir, 'insert-style-preserve-modules'),
// });
});

test('should support output as function', async t => {
Expand Down
22 changes: 22 additions & 0 deletions test/snapshots/test/index.test.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Snapshot report for `test/index.test.ts`

The actual snapshot is saved in `index.test.ts.snap`.

Generated by [AVA](https://avajs.dev).

## should import *.scss and *.sass files with default configuration

> Snapshot 1
'var atualA = "body {\\n color: red;\\n}";var atualB = "body {\\n color: green;\\n}";var atualC = "body {\\n color: blue;\\n}";var index = atualA + atualB + atualC;export { index as default };'

## should insert CSS into head tag

> Snapshot 1
`import ___$insertStyle from '../../../src/insertStyle.js';␊
___$insertStyle("body{color:red}");␊
___$insertStyle("body{color:green}");␊
`
Binary file added test/snapshots/test/index.test.ts.snap
Binary file not shown.

0 comments on commit 634f13b

Please sign in to comment.