diff --git a/common/test/resources/index.mjs b/common/test/resources/index.mjs new file mode 100644 index 0000000000..3152cd4584 --- /dev/null +++ b/common/test/resources/index.mjs @@ -0,0 +1,3 @@ +// Exists to facilitate relative path resolution via Node's require.resolve function. + +export { }; \ No newline at end of file diff --git a/common/web/keyboard-processor/tests/cases/keyboard-properties.js b/common/web/keyboard-processor/tests/cases/keyboard-properties.js index 611cefaf41..16f171ad68 100644 --- a/common/web/keyboard-processor/tests/cases/keyboard-properties.js +++ b/common/web/keyboard-processor/tests/cases/keyboard-properties.js @@ -1,16 +1,26 @@ import { assert } from 'chai'; import fs from 'fs'; +import path from 'path'; + +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); import { KeyboardProperties, SpacebarText } from '@keymanapp/keyboard-processor'; describe('Keyboard Properties', function() { - let rootCommonStubPath = '../../test/resources/json/keyboards/'; + let commonResourcesPackage = '@keymanapp/common-test-resources'; + let commonStubsSubpath = 'json/keyboards'; + let rootCommonStubPath = `${commonResourcesPackage}/${commonStubsSubpath}`; it('initialization from KMW\'s addKeyboards() API spec', () => { - let files = fs.readdirSync(rootCommonStubPath); + // require.resolve cannot resolve a directory directly, unfortunately. + // Needs a file to 'root' the resolution mechanism. See https://github.com/nodejs/node/issues/42219. + const resolvedResourcesPath = path.dirname(require.resolve(`${commonResourcesPackage}/index.mjs`)); + const resolvedCommonStubPath = `${resolvedResourcesPath}/${commonStubsSubpath}`; + let files = fs.readdirSync(resolvedCommonStubPath); for(let file of files) { - let stub = JSON.parse(fs.readFileSync(`${rootCommonStubPath}/${file}`)); + let stub = JSON.parse(fs.readFileSync(`${resolvedCommonStubPath}/${file}`)); let dataset = []; if(stub.languages instanceof Array) { @@ -35,7 +45,7 @@ describe('Keyboard Properties', function() { it('generates display-name text if not directly-specified', () => { // Could convert to run on all stubs, but... this should be fine as-is. - let stub = JSON.parse(fs.readFileSync(`${rootCommonStubPath}/khmer_angkor.json`)); + let stub = JSON.parse(fs.readFileSync(require.resolve(`${rootCommonStubPath}/khmer_angkor.json`))); let propObject = new KeyboardProperties(stub); // Without a configured SpacebarText value, will display the keyboard name. @@ -56,7 +66,7 @@ describe('Keyboard Properties', function() { it('does not override directly-specified display-name text', () => { // Could convert to run on all stubs, but... this should be fine as-is. - let stub = JSON.parse(fs.readFileSync(`${rootCommonStubPath}/khmer_angkor.json`)); + let stub = JSON.parse(fs.readFileSync(require.resolve(`${rootCommonStubPath}/khmer_angkor.json`))); const customDisplayName = "(custom)"; let propObject = new KeyboardProperties(stub);