diff --git a/test/fixtures/dependencies/expected.js b/test/fixtures/dependencies/expected.js new file mode 100644 index 0000000..63f40f8 --- /dev/null +++ b/test/fixtures/dependencies/expected.js @@ -0,0 +1 @@ +export default "body{color:blue}body{color:#fff}body{color:red}"; diff --git a/test/fixtures/dependencies/index.js b/test/fixtures/dependencies/index.js new file mode 100644 index 0000000..817010c --- /dev/null +++ b/test/fixtures/dependencies/index.js @@ -0,0 +1,3 @@ +import style1 from './style1.scss'; + +export default style1; diff --git a/test/fixtures/dependencies/style1.scss b/test/fixtures/dependencies/style1.scss new file mode 100644 index 0000000..6c75562 --- /dev/null +++ b/test/fixtures/dependencies/style1.scss @@ -0,0 +1,3 @@ +@import 'style2.scss'; + +body {color: red;} diff --git a/test/fixtures/dependencies/style2.scss b/test/fixtures/dependencies/style2.scss new file mode 100644 index 0000000..b3623d7 --- /dev/null +++ b/test/fixtures/dependencies/style2.scss @@ -0,0 +1,3 @@ +@import 'style3.scss'; + +body {color: white;} diff --git a/test/fixtures/dependencies/style3.scss b/test/fixtures/dependencies/style3.scss new file mode 100644 index 0000000..355275b --- /dev/null +++ b/test/fixtures/dependencies/style3.scss @@ -0,0 +1 @@ +body {color:blue;} diff --git a/test/index.test.ts b/test/index.test.ts index a232f62..5d190f4 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -478,3 +478,68 @@ test('When `sourcemap` is set, to `true`, adjacent source map file should be out }); }); }); + +test('module stylesheets graph should be added to watch list', t => { + const inputFilePath = 'test/fixtures/dependencies/index.js'; + + // Bundle our dependencies fixture module + // ---- + return rollup({ + input: inputFilePath, + plugins: [ + sass({ + options: sassOptions + }) + ] + }) + // Load nested style sheet contents and return associated list of filename and content tuples + // ---- + .then(bundle => { + return Promise.all([ + 'test/fixtures/dependencies/style1.scss', + 'test/fixtures/dependencies/style2.scss', + 'test/fixtures/dependencies/style3.scss', + ] + .map(filePath => fs.readFile(filePath).then(buf => [filePath, squash(buf.toString())])) + ) + // Run tests + // ---- + .then(async nestedFilePathsAndContents => { + // Check `watchFiles` count (three above, and 'index.js' module one) + t.true(bundle.watchFiles.length === 4, 'should contain expected number of "watched" files'); + + // Ensure our initial 'index.js' module is being watched + t.true(bundle.watchFiles[0].endsWith(inputFilePath), + 'Expected `bundle.watchFiles[0]` to end with "index.js"'); + + // Skip 'index.js' file and ensure remaining nested files are also watched. + // ---- + bundle.watchFiles.slice(1).forEach((filePath, i) => { + const [expectedTail] = nestedFilePathsAndContents[i]; + t.true(filePath.endsWith(expectedTail), `${filePath} should end with ${expectedTail}`); + }); + + // Get target module. + // ---- + const targetModule = bundle?.cache?.modules[0]; + t.true(!!targetModule, 'Expected bundle data'); + + // Ensure target module transform dependencies indeed end with expected file path tails. + // ---- + t.true(targetModule.transformDependencies?.every((filePath, i) => { + const [expectedTail] = nestedFilePathsAndContents[i]; + const result = filePath.endsWith(expectedTail); + t.true(result, `${filePath} should end with ${expectedTail}`); + return result; + }), '`bundle.cache.modules[0].transformDependencies` entries should' + + ' each end with expected file-path tails'); + + // Test final content output + // ---- + const expectedFinalContent = await fs.readFile('test/fixtures/dependencies/expected.js') + .then(x => x.toString()); + + t.is(targetModule.code.trim(), expectedFinalContent.trim()); + }); + }); +});