Skip to content

Commit

Permalink
test:unified tests
Browse files Browse the repository at this point in the history
  • Loading branch information
idoros committed Nov 20, 2023
1 parent aec07a0 commit f3b1f59
Showing 1 changed file with 57 additions and 71 deletions.
128 changes: 57 additions & 71 deletions packages/cli/test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createTempDirectory,
ITempDirectory,
} from '@stylable/e2e-test-kit';
import { nodeFs } from '@file-services/node';
import { STImport, STVar } from '@stylable/core/dist/features';
import { diagnosticBankReportToStrings } from '@stylable/core-test-kit';

Expand Down Expand Up @@ -137,87 +138,72 @@ describe('Stylable Cli', function () {
]);
});

it('single file build with default ns-resolver', () => {
it('should resolve resolveNamespace with defaults: cli-arg -> stylable.config -> default', () => {
populateDirectorySync(tempDir.path, {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'style.st.css': `.root{color:red}`,
});

const nsr = require.resolve('@stylable/node');
runCliSync(['--rootDir', tempDir.path, '--nsr', nsr, '--cjs']);
{
// default
runCliSync(['--rootDir', tempDir.path, '--cjs']);

const dirContent = loadDirSync(tempDir.path);

expect(
evalStylableModule<{ namespace: string }>(
dirContent['style.st.css.js'],
'style.st.css.js'
).namespace
).equal(resolveNamespace('style', join(tempDir.path, 'style.st.css')));
});

it('should use the default to stylable.config resolveNamespace', () => {
populateDirectorySync(tempDir.path, {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'stylable.config.js': `
let c = 0;
module.exports = {
defaultConfig(fs) {
return {
resolveNamespace: () => 'config-ns-' + c++
};
},
};
`,
'style.st.css': `.root{color:red}`,
});

runCliSync(['--rootDir', tempDir.path, '--cjs']);

const dirContent = loadDirSync(tempDir.path);

expect(
evalStylableModule<{ namespace: string }>(
dirContent['style.st.css.js'],
'style.st.css.js'
).namespace
).equal('config-ns-0');
});
expect(
evalStylableModule<{ namespace: string }>(
loadDirSync(tempDir.path)['style.st.css.js'],
'style.st.css.js'
).namespace,
'default'
).equal(resolveNamespace('style', join(tempDir.path, 'style.st.css')));
}
{
// stylable.config
nodeFs.writeFileSync(
join(tempDir.path, 'stylable.config.js'),
`
let c = 0;
module.exports = {
defaultConfig(fs) {
return {
resolveNamespace: () => 'config-ns-' + c++
};
},
};
`
);

it('should use explicit namespaceResolver argument over stylable.config resolveNamespace', () => {
populateDirectorySync(tempDir.path, {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'stylable.config.js': `
let c = 0;
module.exports = {
defaultConfig(fs) {
return {
resolveNamespace: () => 'config-ns-' + c++
};
},
};
`,
'custom-ns-resolver.js': `
let c = 0;
module.exports.resolveNamespace = function resolveNamespace() {
return 'custom-ns-' + c++;
}
`,
'style.st.css': `.root{color:red}`,
});
runCliSync(['--rootDir', tempDir.path, '--cjs']);

runCliSync(['--rootDir', tempDir.path, '--nsr', './custom-ns-resolver.js', '--cjs']);
expect(
evalStylableModule<{ namespace: string }>(
loadDirSync(tempDir.path)['style.st.css.js'],
'style.st.css.js'
).namespace,
'stylable.config'
).equal('config-ns-0');
}
{
// cli argument
nodeFs.writeFileSync(
join(tempDir.path, 'custom-ns-resolver.js'),
`
let c = 0;
module.exports.resolveNamespace = function resolveNamespace() {
return 'custom-ns-' + c++;
}
`
);

const dirContent = loadDirSync(tempDir.path);
runCliSync(['--rootDir', tempDir.path, '--nsr', './custom-ns-resolver.js', '--cjs']);

expect(
evalStylableModule<{ namespace: string }>(
dirContent['style.st.css.js'],
'style.st.css.js'
).namespace
).equal('custom-ns-0');
expect(
evalStylableModule<{ namespace: string }>(
loadDirSync(tempDir.path)['style.st.css.js'],
'style.st.css.js'
).namespace,
'cli argument'
).equal('custom-ns-0');
}
});

it('build .st.css source files with namespace reference', () => {
Expand Down

0 comments on commit f3b1f59

Please sign in to comment.