mathjs/test/node-tests/defaultInstance.test.js
Jos de Jong 6f00715754
Specify import require paths (continuation of #1941) (#1962)
* Add `.js` extension to source file imports

* Specify package `exports` in `package.json`

Specify package type as `commonjs` (It's good to be specific)

* Move all compiled scripts into `lib` directory

Remove ./number.js (You can use the compiled ones in `./lib/*`)

Tell node that the `esm` directory is type `module` and enable tree shaking.

Remove unused files from packages `files` property

* Allow importing of package.json

* Make library ESM first

* - Fix merge conflicts
- Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs`
- Refactor unit tests to be able to run with plain nodejs (no transpiling)
- Fix browser examples

* Fix browser and browserstack tests

* Fix running unit tests on Node 10 (which has no support for modules)

* Fix node.js examples (those are still commonjs)

* Remove the need for `browserBundle.cjs`

* Generate minified bundle only

* [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.**
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Cleanup console.log

* Add integration tests to test the entry points (commonjs/esm, full/number only)

* Create backward compatibility error messages in the files moved/removed since v8

* Describe breaking changes in HISTORY.md

* Bump karma from 5.2.1 to 5.2.2 (#1965)

Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-09-20 18:01:29 +02:00

151 lines
4.7 KiB
JavaScript

const assert = require('assert')
const cp = require('child_process')
const path = require('path')
const math = require('../../lib/cjs/defaultInstance.js').default
const version = require('../../package.json').version
describe('defaultInstance', function () {
it('should get a default instance of mathjs', function () {
assert.strictEqual(typeof math, 'object')
assert.deepStrictEqual(math.config(), {
matrix: 'Matrix',
number: 'number',
precision: 64,
predictable: false,
epsilon: 1e-12,
randomSeed: null
})
})
it('should create an instance of math.js with custom configuration', function () {
const math1 = math.create({
matrix: 'Array',
number: 'BigNumber'
})
assert.strictEqual(typeof math1, 'object')
assert.deepStrictEqual(math1.config(), {
matrix: 'Array',
number: 'BigNumber',
precision: 64,
predictable: false,
epsilon: 1e-12,
randomSeed: null
})
})
it('two instances of math.js should be isolated from each other', function () {
const math1 = math.create()
const math2 = math.create({
matrix: 'Array'
})
assert.notStrictEqual(math, math1)
assert.notStrictEqual(math, math2)
assert.notStrictEqual(math1, math2)
assert.notDeepStrictEqual(math1.config(), math2.config())
assert.notDeepStrictEqual(math.config(), math2.config())
// changing config should not affect the other
math1.config({ number: 'BigNumber' })
assert.strictEqual(math.config().number, 'number')
assert.strictEqual(math1.config().number, 'BigNumber')
assert.strictEqual(math2.config().number, 'number')
})
it('should apply configuration using the config function', function () {
const math1 = math.create()
assert.deepStrictEqual(math1.sqrt(-4), math1.complex(0, 2))
assert.strictEqual(math1.typeOf(math1.pi), 'number')
assert.strictEqual(math1.typeOf(math1.Unit.UNITS.rad.value), 'number') // TODO: find a better way to unit test this
assert.strictEqual(math1.bignumber(1).div(3).toString(), '0.3333333333333333333333333333333333333333333333333333333333333333')
const config = math1.config({
number: 'BigNumber',
precision: 4,
predictable: true
})
assert.deepStrictEqual(config, {
matrix: 'Matrix',
number: 'BigNumber',
precision: 4,
predictable: true,
epsilon: 1e-12,
randomSeed: null
})
assert.ok(math1.isNaN(math1.sqrt(-4)))
assert.strictEqual(math1.typeOf(math1.pi), 'BigNumber')
assert.strictEqual(math1.typeOf(math1.Unit.UNITS.rad.value), 'BigNumber') // TODO: find a better way to unit test this
assert.strictEqual(math1.bignumber(1).div(3).toString(), '0.3333')
const config2 = math1.config({
number: 'number',
precision: 64,
predictable: false
})
assert.deepStrictEqual(config2, {
matrix: 'Matrix',
number: 'number',
precision: 64,
predictable: false,
epsilon: 1e-12,
randomSeed: null
})
assert.deepStrictEqual(math1.sqrt(-4), math1.complex(0, 2))
assert.strictEqual(math1.typeOf(math1.pi), 'number')
assert.strictEqual(math1.typeOf(math1.Unit.UNITS.rad.value), 'number') // TODO: find a better way to unit test this
assert.strictEqual(math1.bignumber(1).div(3).toString(), '0.3333333333333333333333333333333333333333333333333333333333333333')
})
it('should not override a custom imported function when config changes', function () {
const math1 = math.create()
math1.import({
sqrt: function customSqrt (x) {
return 'foo(' + x + ')'
}
}, { override: true })
assert.strictEqual(math1.sqrt(4), 'foo(4)')
// changing config should not change the custom function sqrt
math1.config({ number: 'BigNumber' })
assert.strictEqual(math1.sqrt(4), 'foo(4)')
})
// TODO: test whether the namespace is correct: has functions like sin, constants like pi, objects like type and error.
it('should throw an error when ES5 is not supported', function () {
const create = Object.create
Object.create = undefined // fake missing Object.create function
assert.throws(function () {
math.create()
}, /ES5 not supported/)
// restore Object.create
Object.create = create
})
it('should have the correct version number', function () {
const math = require('../..')
assert.strictEqual(math.version, version)
})
it('should be robust against pollution of the Object prototype', function (done) {
const filename = path.join(__dirname, 'pollutedObjectPrototype.js')
cp.exec('node ' + filename, function (error, result) {
assert.strictEqual(error, null)
assert.strictEqual(result, '2i\n')
done()
})
})
})