Skip to content

Commit

Permalink
Convert tests to ESM
Browse files Browse the repository at this point in the history
Convert all the tests to ESM (`.mjs`) in preparation for Chai 5, which drops support for CommonJS.
- replace `require` with `import` throughout.
- create a global `chai` object to replace the old `chai = require('chai')`.
- rename tests from '.js' to '.mjs'.
  • Loading branch information
eatyourgreens committed Jan 11, 2024
1 parent e4c8770 commit ab39c84
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 65 deletions.
12 changes: 3 additions & 9 deletions test/client.js → test/client.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
var MockPrimus, SugarClient, chai, expect,
hasProp = {}.hasOwnProperty;
import SugarClient from '../lib/client.js';
import MockPrimus from './support/mock_primus.js';

chai = require('chai');

expect = chai.expect;

SugarClient = require('../lib/client');

MockPrimus = require('./support/mock_primus');
const hasProp = {}.hasOwnProperty;

describe('SugarClient', function() {
var callback1, callback2, originalConsole, sessionClient, setupEvents, userClient;
Expand Down
26 changes: 16 additions & 10 deletions test/index.js → test/index.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import RedisClient from '../lib/redis_client.js';
import * as baseChai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import chaiHttp from 'chai-http';
import chaiSpies from 'chai-spies';
import chaiChanges from 'chai-changes';

process.env.NEW_RELIC_ENABLED = false;

process.env.NEW_RELIC_NO_CONFIG_FILE = true;
Expand All @@ -14,20 +21,19 @@ process.env.SUGAR_TALK_PASSWORD = 'testPass';

process.env.PANOPTES_HOST = 'http://sugar_test.panoptes';

const RedisClient = require('../lib/redis_client');

const redis = new RedisClient();
redis.connect();

const chai = require('chai');

chai.use(require('chai-as-promised'));

chai.use(require('chai-http'));

chai.use(require('chai-spies'));
const chai = {
...baseChai,
...baseChai.use(chaiAsPromised),
...baseChai.use(chaiHttp),
...baseChai.use(chaiSpies),
...baseChai.use(chaiChanges)
};

chai.use(require('chai-changes'));
global.chai = chai;
global.expect = baseChai.expect;

beforeEach(function() {
return redis.flushAll();
Expand Down
12 changes: 4 additions & 8 deletions test/panoptes.js → test/panoptes.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const chai = require('chai');
const jwt = require('jsonwebtoken');
const Sentry = require('@sentry/node');

const expect = chai.expect;

const Panoptes = require('../lib/panoptes');
import jwt from 'jsonwebtoken';
import * as Sentry from '@sentry/node';
import Panoptes from '../lib/panoptes.js';

describe('Panoptes', function() {
return describe('#authenticator', function() {
Expand All @@ -28,7 +24,7 @@ describe('Panoptes', function() {
jwt.verify = chai.spy(() => {
throw new Error('invalid token');
});
Sentry.captureException = chai.spy();
chai.spy.on(Sentry, 'captureException');
return Panoptes.authenticator(1, 'invalid_auth').then(function(response) {
expect(response.status).to.equal(401);
expect(response.success).to.be.false;
Expand Down
10 changes: 2 additions & 8 deletions test/presence.js → test/presence.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
var Presence, chai, expect, presence;
import Presence from '../lib/presence.js';

chai = require('chai');

expect = chai.expect;

Presence = require('../lib/presence');

presence = new Presence();
let presence = new Presence();

describe('Presence', function() {
var active, activeOn, createSamples, inactive;
Expand Down
10 changes: 2 additions & 8 deletions test/pub_sub.js → test/pub_sub.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
var PubSub, chai, expect, pubSub;
import PubSub from '../lib/pub_sub.js';

chai = require('chai');

expect = chai.expect;

PubSub = require('../lib/pub_sub');

pubSub = new PubSub();
let pubSub = new PubSub();

describe('PubSub', function() {
var subscribeTo, thennableSpy;
Expand Down
11 changes: 3 additions & 8 deletions test/server.js → test/server.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
var SugarServer, chai, expect;
const jwt = require('jsonwebtoken');

chai = require('chai');

expect = chai.expect;

SugarServer = require('./support/sugar_server');
import jwt from 'jsonwebtoken';
import URL from 'url';
import SugarServer from './support/sugar_server.js';

describe('Server', function() {
var client, sugar;
Expand Down
11 changes: 3 additions & 8 deletions test/server_http_api.js → test/server_http_api.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const chai = require('chai');
const jwt = require('jsonwebtoken');

const expect = chai.expect;

const URL = require('url');

const SugarServer = require('./support/sugar_server');
import jwt from 'jsonwebtoken';
import URL from 'url';
import SugarServer from './support/sugar_server.js';

describe('Server HTTP API', function() {
let sugar = null;
Expand Down
9 changes: 3 additions & 6 deletions test/server_socket_api.js → test/server_socket_api.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
const chai = require('chai');
const jwt = require('jsonwebtoken');

const expect = chai.expect;

const SugarServer = require('./support/sugar_server');
import jwt from 'jsonwebtoken';
import URL from 'url';
import SugarServer from './support/sugar_server.js';

describe('Server Socket API', function() {
var client, sugar;
Expand Down

0 comments on commit ab39c84

Please sign in to comment.