Skip to content

Commit

Permalink
Merge pull request #129 from elycruz/issue-#29-rollup_watch_feature
Browse files Browse the repository at this point in the history
issue-#29 - Added test for rollup watch feature
  • Loading branch information
elycruz committed Apr 17, 2024
2 parents 061ed45 + bb10369 commit bd552b4
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/fixtures/dependencies/expected.js
@@ -0,0 +1 @@
export default "body{color:blue}body{color:#fff}body{color:red}";
3 changes: 3 additions & 0 deletions test/fixtures/dependencies/index.js
@@ -0,0 +1,3 @@
import style1 from './style1.scss';

export default style1;
3 changes: 3 additions & 0 deletions test/fixtures/dependencies/style1.scss
@@ -0,0 +1,3 @@
@import 'style2.scss';

body {color: red;}
3 changes: 3 additions & 0 deletions test/fixtures/dependencies/style2.scss
@@ -0,0 +1,3 @@
@import 'style3.scss';

body {color: white;}
1 change: 1 addition & 0 deletions test/fixtures/dependencies/style3.scss
@@ -0,0 +1 @@
body {color:blue;}
65 changes: 65 additions & 0 deletions test/index.test.ts
Expand Up @@ -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());
});
});
});

0 comments on commit bd552b4

Please sign in to comment.