From d09056c7b01dbd9aa59d2c7f84a64f3458e46742 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Sat, 30 May 2020 06:58:11 -0700 Subject: [PATCH] chore: add test for loading via AMD --- .karma.config.js | 5 +++-- package-lock.json | 6 ++++++ package.json | 1 + test/slug.test.js | 21 ++++++++++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/.karma.config.js b/.karma.config.js index efcc8dd..e42ae03 100644 --- a/.karma.config.js +++ b/.karma.config.js @@ -3,10 +3,11 @@ module.exports = function (config) { frameworks: ['mocha', 'chai'], files: [ 'slug.js', - 'test/**/*.js' + 'node_modules/requirejs/require.js', // Used to test loading via AMD. + 'test/**/*.js', ], preprocessors: { 'slug.js': 'coverage' }, - reporters: ['coverage'], + reporters: ['progress', 'coverage'], coverageReporter: { dir: '.nyc_output', reporters: [ diff --git a/package-lock.json b/package-lock.json index 80824b6..61cfc81 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3928,6 +3928,12 @@ "integrity": "sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==", "dev": true }, + "requirejs": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.6.tgz", + "integrity": "sha512-ipEzlWQe6RK3jkzikgCupiTbTvm4S0/CAU5GlgptkN5SO6F3u0UD0K18wy6ErDqiCyP4J4YYe1HuAShvsxePLg==", + "dev": true + }, "requires-port": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz", diff --git a/package.json b/package.json index c9336a2..4e88084 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "karma-mocha": "^2.0.1", "mocha": "^7.0.0", "nyc": "^15.0.1", + "requirejs": "^2.3.6", "standard": "^14.3.3" }, "licenses": [ diff --git a/test/slug.test.js b/test/slug.test.js index 62aaa24..405b212 100644 --- a/test/slug.test.js +++ b/test/slug.test.js @@ -1,7 +1,9 @@ /* global beforeEach, chai, describe, it */ +const inBrowser = typeof window !== 'undefined' + describe('slug', function () { - const slug = (typeof window !== 'undefined' && window.slug) || require('../slug') + const slug = (inBrowser && window.slug) || require('../slug') const assert = typeof chai === 'undefined' ? require('assert') : chai.assert beforeEach(slug.reset) @@ -959,4 +961,21 @@ describe('slug', function () { it('should handle a lone high surrogate by itself', () => { assert.strictEqual(slug(String.fromCodePoint(55296)), 'ia') }) + + it('should be able to be loaded via amd', (done) => { + const modulePath = inBrowser ? 'base/slug' : '../slug' + const requirejs = (inBrowser && window.requirejs) || require('requirejs') + + if (!inBrowser) { + requirejs.config({ + nodeRequire: require + }) + } + + requirejs([modulePath], function (amdSlug) { + // Use toString() to compare source code. + assert.deepStrictEqual(amdSlug.toString(), slug.toString()) + done() + }) + }) })