Skip to content

Commit

Permalink
chore: migrate from nodeunit to jest (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesine authored Jan 17, 2025
1 parent a8e54fd commit 0e0671e
Show file tree
Hide file tree
Showing 30 changed files with 3,318 additions and 2,253 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 22.x]
node-version: [22.x]

env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
Expand All @@ -39,10 +39,7 @@ jobs:

- name: Run tests
run: npm run coverage

- name: Run Coveralls
run: npm run coveralls


- name: Check for changed dist files
run: |
git fetch origin main --depth=1
Expand All @@ -53,4 +50,8 @@ jobs:
echo -e "\033[31mChanged files:"
echo "$DIST_CHANGED"
exit 1
fi
fi
- name: Run Coveralls
run: npm run coveralls
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ CSS_MIN=$(NODE_MODULES)/.bin/cleancss
JS_MIN=$(NODE_MODULES)/.bin/uglifyjs
JS_HINT=$(NODE_MODULES)/.bin/jshint
D3=$(NODE_MODULES)/d3
JSDOM=$(NODE_MODULES)/jsdom
NODEUNIT=$(NODE_MODULES)/nodeunit

CSS_FILES=\
src/css/detail.css\
Expand Down Expand Up @@ -61,7 +59,7 @@ build: rickshaw.min.css rickshaw.min.js
clean:
rm -rf rickshaw.css rickshaw.js rickshaw.min.*

test: $(D3) $(JSDOM) $(NODEUNIT)
test: $(D3)
npm test

$(JS_HINT):
Expand All @@ -76,12 +74,6 @@ $(JS_MIN):
$(D3):
npm install d3

$(JSDOM):
npm install jsdom

$(NODEUNIT):
npm install nodeunit

rickshaw.css: $(CSS_FILES)
cat $(CSS_FILES) > rickshaw.css

Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ Rickshaw relies on the fantastic [D3 visualization library](http://mbostock.gith

Some extensions require [jQuery](http://jquery.com) and [jQuery UI](http://jqueryui.com), but for drawing some basic graphs you'll be okay without.

Rickshaw uses [jsdom](https://github.com/tmpvar/jsdom) to run unit tests in Node to be able to do SVG manipulation. As of the jsdom 7.0.0 release, jsdom requires Node.js 4 or newer [jsdom changelog](https://github.com/tmpvar/jsdom/blob/master/Changelog.md#700). If you want to run the tests on your machine, and you don't have access to a version of node >= 4.0, you can `npm install jsdom@3` so that you can run the tests using the [3.x branch of jsdom](https://github.com/tmpvar/jsdom/tree/3.x).

## Rickshaw.Graph

Expand Down
24 changes: 24 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
testEnvironment: 'jsdom',
moduleDirectories: ['node_modules'],
testMatch: ['**/**/*.test.js'],
collectCoverageFrom: [
'Rickshaw.*.js',
'rickshaw.js',
'!rickshaw.min.js',
'!**/node_modules/**',
],
coverageThreshold: {
global: {
branches: 59,
functions: 62,
lines: 67,
statements: 66,
}
},
setupFiles: ['./jest.setup.js'],
transform: {},
testEnvironmentOptions: {
url: 'http://localhost/'
}
};
4 changes: 4 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Set up any global variables needed for testing
global.d3 = require('d3');

// No need to extend expect here as we're not using custom matchers yet
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@
"clean-css-cli": "^4.3.0",
"coveralls": "^2.11.9",
"istanbul": "^0.4.3",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jquery": "^3.2.1",
"jsdom": "^8.1.0",
"jshint": "^2.9.5",
"nodemon": "^1.11.0",
"nodeunit": "^0.9.1",
"sinon": "^2.3.8",
"uglify-js": "^2.8.29"
},
"scripts": {
"build": "make clean && make",
"examples": "open examples/index.html",
"lint": "jshint src/js/*",
"test": "make && nodeunit tests",
"test": "jest",
"test:watch": "jest --watch",
"watch": "nodemon --watch src --exec make rickshaw.js",
"coverage": "istanbul cover nodeunit tests --reporter=lcov",
"coverage": "jest --coverage",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"preversion:src": "sed \"s/version: '[^,]*'/version: '$npm_package_version'/\" src/js/Rickshaw.js > output && mv output src/js/Rickshaw.js",
"preversion:bower": "sed 's/\"version\": \"[^,]*\"/\"version\": \"'$npm_package_version'\"/' bower.json > output && mv output bower.json",
Expand Down
109 changes: 57 additions & 52 deletions tests/Rickshaw.Class.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
var Rickshaw = require('../rickshaw');

exports.load = function(test) {

test.equal(typeof Rickshaw.Class, 'object', 'Rickshaw.Class is a function');
test.done();
};

exports.instantiation = function(test) {

Rickshaw.namespace('Rickshaw.Sample.Class');

Rickshaw.Sample.Class = Rickshaw.Class.create({
name: 'sample',
concat: function(suffix) {
return [this.name, suffix].join(' ');
}
});

var sample = new Rickshaw.Sample.Class();
test.equal(sample.concat('polka'), 'sample polka');

Rickshaw.namespace('Rickshaw.Sample.Class.Prefix');

Rickshaw.Sample.Subclass = Rickshaw.Class.create( Rickshaw.Sample.Class, {
name: 'sampler'
});

var sampler = new Rickshaw.Sample.Subclass();
test.equal(sampler.concat('polka'), 'sampler polka');

test.done();
};

exports.array = function(test) {

Rickshaw.namespace('Rickshaw.Sample.Array');

Rickshaw.Sample.Array = Rickshaw.Class.create(Array, {
second: function() {
return this[1];
}
});

var array = new Rickshaw.Sample.Array();
array.push('red');
array.push('blue');

test.equal(array.second(), 'blue');

test.done();
};
const Rickshaw = require('../rickshaw');

describe('Rickshaw.Class', () => {
test('should be defined as an object', () => {
expect(typeof Rickshaw.Class).toBe('object');
});

describe('instantiation', () => {
test('should create a basic class instance', () => {
// Create fresh class definition for this test
const TestClass = Rickshaw.Class.create({
name: 'sample',
concat: function(suffix) {
return [this.name, suffix].join(' ');
}
});

const sample = new TestClass();
expect(sample.concat('polka')).toBe('sample polka');
});

test('should create a subclass instance', () => {
// Create fresh parent class for this test
const ParentClass = Rickshaw.Class.create({
name: 'sample',
concat: function(suffix) {
return [this.name, suffix].join(' ');
}
});

// Create fresh subclass for this test
const SubClass = Rickshaw.Class.create(ParentClass, {
name: 'sampler'
});

const sampler = new SubClass();
expect(sampler.concat('polka')).toBe('sampler polka');
});
});

describe('array inheritance', () => {
test('should extend Array functionality', () => {
// Create fresh array class for this test
const TestArray = Rickshaw.Class.create(Array, {
second: function() {
return this[1];
}
});

const array = new TestArray();
array.push('red');
array.push('blue');

expect(array.second()).toBe('blue');
});
});
});
Loading

0 comments on commit 0e0671e

Please sign in to comment.