migrate tests to node:test
This commit is contained in:
+25
-28
@@ -1,18 +1,15 @@
|
||||
import { describe, it, before, after } from 'node:test';
|
||||
/* jslint node:true */
|
||||
|
||||
import BoxError from '../boxerror.js';
|
||||
import common from './common.js';
|
||||
import expect from 'expect.js';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import paths from '../paths.js';
|
||||
import safe from 'safetydance';
|
||||
import tasks from '../tasks.js';
|
||||
import _ from '../underscore.js';
|
||||
|
||||
/* global it:false */
|
||||
/* global before:false */
|
||||
/* global after:false */
|
||||
/* global describe:false */
|
||||
|
||||
describe('task', function () {
|
||||
const { setup, cleanup } = common;
|
||||
@@ -31,18 +28,18 @@ describe('task', function () {
|
||||
|
||||
it('add succeeds', async function () {
|
||||
const id = await tasks.add(TASK.type, TASK.args);
|
||||
expect(id).to.be.ok();
|
||||
assert.ok(id);
|
||||
taskId = id;
|
||||
});
|
||||
|
||||
it('get succeeds', async function () {
|
||||
const task = await tasks.get(taskId);
|
||||
expect(_.pick(task, Object.keys(TASK))).to.eql(TASK);
|
||||
assert.deepEqual(_.pick(task, Object.keys(TASK)), TASK);
|
||||
});
|
||||
|
||||
it('get random task fails', async function () {
|
||||
const task = await tasks.get('random');
|
||||
expect(task).to.be(null);
|
||||
assert.equal(task, null);
|
||||
});
|
||||
|
||||
it('update succeeds', async function () {
|
||||
@@ -50,35 +47,35 @@ describe('task', function () {
|
||||
TASK.message = 'almost ther';
|
||||
await tasks.update(taskId, { percent: TASK.percent, message: TASK.message });
|
||||
const task = await tasks.get(taskId);
|
||||
expect(_.pick(task, Object.keys(TASK))).to.eql(TASK);
|
||||
assert.deepEqual(_.pick(task, Object.keys(TASK)), TASK);
|
||||
});
|
||||
|
||||
it('list succeeds - does not exist', async function () {
|
||||
const result = await tasks.list(1, 1, { type: 'randomtask' });
|
||||
expect(result.length).to.be(0);
|
||||
assert.equal(result.length, 0);
|
||||
});
|
||||
|
||||
it('list succeeds - by type', async function () {
|
||||
const result = await tasks.list(1, 1, { type: TASK.type });
|
||||
expect(result.length).to.be(1);
|
||||
expect(_.pick(result[0], Object.keys(TASK))).to.eql(TASK);
|
||||
assert.equal(result.length, 1);
|
||||
assert.deepEqual(_.pick(result[0], Object.keys(TASK)), TASK);
|
||||
});
|
||||
|
||||
it('list succeeds - all', async function () {
|
||||
const result = await tasks.list(1, 1, { type: null });
|
||||
expect(result.length).to.be(1);
|
||||
expect(_.pick(result[0], Object.keys(TASK))).to.eql(TASK);
|
||||
assert.equal(result.length, 1);
|
||||
assert.deepEqual(_.pick(result[0], Object.keys(TASK)), TASK);
|
||||
});
|
||||
|
||||
it('del succeeds', async function () {
|
||||
await tasks._del(taskId);
|
||||
const task = await tasks.get(taskId);
|
||||
expect(task).to.be(null);
|
||||
assert.equal(task, null);
|
||||
});
|
||||
|
||||
it('del missing task fails', async function () {
|
||||
const [error] = await safe(tasks._del('1235'));
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
assert.equal(error.reason, BoxError.NOT_FOUND);
|
||||
});
|
||||
|
||||
it('can run valid task - success', async function () {
|
||||
@@ -86,7 +83,7 @@ describe('task', function () {
|
||||
|
||||
const [error, result] = await safe(tasks.startTask(successTaskId, {}));
|
||||
if (error) throw error;
|
||||
expect(result).to.equal('ping');
|
||||
assert.equal(result, 'ping');
|
||||
});
|
||||
|
||||
it('can run valid task - error', async function () {
|
||||
@@ -94,8 +91,8 @@ describe('task', function () {
|
||||
|
||||
const [error, result] = await safe(tasks.startTask(errorTaskId, {}));
|
||||
if (!error) throw new Error('expecting task to fail');
|
||||
expect(error.message).to.be('Task crashed. Failed for arg: ping');
|
||||
expect(result).to.not.be.ok();
|
||||
assert.equal(error.message, 'Task crashed. Failed for arg: ping');
|
||||
assert.ok(!(result));
|
||||
});
|
||||
|
||||
it('can get logs of crash', async function () {
|
||||
@@ -103,11 +100,11 @@ describe('task', function () {
|
||||
|
||||
const [error, result] = await safe(tasks.startTask(crashTaskId, {}));
|
||||
if (!error) throw new Error('expecting task to crash');
|
||||
expect(error.message).to.contain(`Task ${crashTaskId} crashed`);
|
||||
expect(result).to.not.be.ok();
|
||||
assert.ok(error.message.includes(`Task ${crashTaskId} crashed`));
|
||||
assert.ok(!(result));
|
||||
|
||||
const logs = fs.readFileSync(`${paths.TASKS_LOG_DIR}/${crashTaskId}.log`, 'utf8');
|
||||
expect(logs).to.contain('Crashing for arg: ping');
|
||||
assert.ok(logs.includes('Crashing for arg: ping'));
|
||||
});
|
||||
|
||||
it('can stop task', async function () {
|
||||
@@ -119,9 +116,9 @@ describe('task', function () {
|
||||
|
||||
const [error, result] = await safe(tasks.startTask(sleepTaskId, {}));
|
||||
if (!error) throw new Error('expecting task to stop');
|
||||
expect(error.message).to.contain('stopped');
|
||||
expect(error.code).to.be(tasks.ESTOPPED);
|
||||
expect(result).to.not.be.ok();
|
||||
assert.ok(error.message.includes('stopped'));
|
||||
assert.equal(error.code, tasks.ESTOPPED);
|
||||
assert.ok(!(result));
|
||||
});
|
||||
|
||||
it('task timesout', async function () {
|
||||
@@ -129,8 +126,8 @@ describe('task', function () {
|
||||
|
||||
const [error, result] = await safe(tasks.startTask(timeoutTaskId, { timeout: 2000 }));
|
||||
if (!error) throw new Error('expecting task to timeout');
|
||||
expect(error.code).to.be(tasks.ETIMEOUT);
|
||||
expect(error.message).to.contain('timed out');
|
||||
expect(result).to.not.be.ok();
|
||||
assert.equal(error.code, tasks.ETIMEOUT);
|
||||
assert.ok(error.message.includes('timed out'));
|
||||
assert.ok(!(result));
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user