How to use the rejects function from assert

Find comprehensive JavaScript assert.rejects code examples handpicked from public code repositorys.

assert.rejects verifies that a function call promise is rejected and rejects it if not.

605
606
607
608
609
610
611
612
613
614
let deferred = new tester.Thenable();
let value = {};
deferred.reject(value);
deferred.resolve(123);
deferred.reject(12345);
await assert.rejects(async () => {
  await deferred;
}, err => {
  assert.strictEqual(err, value);
  return true;
fork icon9
star icon93
watch icon6

60
61
62
63
64
65
66
67
68
69
70
71
    });
  });


  it('should require valid region size', async () => {
    subject = new iiif.Processor(`${base}/0,0,0,0/full/0/default.png`, streamResolver);
    assert.rejects(() => subject.execute(), iiif.IIIFError);
  });
});


describe('size', () => {
fork icon4
star icon21
watch icon55

+ 7 other calls in file

How does assert.rejects work?

assert.rejects is a function in Node.js that verifies if a given Promise rejects (throws an error), and if it does not, it throws an AssertionError. When passed a function that returns a Promise, assert.rejects waits for the Promise to reject, and if it does not, the assertion fails. It also allows you to assert on the error message, type or other properties of the rejected error.

1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
});


describe('LCHAPIExecuteComposition', function test_LCHAPIExecuteComposition() {


	it('rejects if param1 not LCHComposition', async function() {
		await rejects(mod.LCHAPIExecuteComposition({}, kTesting.StubAPIObjectValid()), /LCHErrorInputNotValid/);
	});


	it('rejects if param2 not API object', async function() {
		await rejects(mod.LCHAPIExecuteComposition(kTesting.StubCompositionObjectValid(), {}), /LCHErrorInputNotValid/);
fork icon2
star icon37
watch icon2

+ 4 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
16
const LCHDocument = require('../LCHDocument/main.js').default;


describe('LCHTransportImport', function test_LCHTransportImport() {


	it('rejects if not object', async function () {
		await rejects(ZDRTestingWrap.App.LCHTransport.LCHTransportImport(null), /LCHErrorInputNotValid/);
	});


	it('returns object', async function () {
		deepEqual(await ZDRTestingWrap.App.LCHTransport.LCHTransportImport({}), {});
fork icon2
star icon37
watch icon2

+ 4 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const assert = require("assert");

async function asyncFn() {
  throw new Error("Async function error");
}

assert.rejects(
  asyncFn(),
  { name: "Error", message: "Async function error" },
  "The function should throw the expected error"
);

In this example, assert.rejects is used to test whether an async function asyncFn throws an expected error. The method takes three arguments: the async function being tested, the expected error object (with the error's name and message properties), and an optional error message to include if the assertion fails.

4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
    { name: 'test-1' },
    { name: 'test-2' }
  ];

  await Model.create(arr);
  await assert.rejects(() => Model.deleteOne([]), /must be an object/);
  const docs = await Model.find({});

  assert.equal(docs.length, 2);
});
fork icon0
star icon0
watch icon494

167
168
169
170
171
172
173
174
175
176
    { cnames: {}, domains: {}, entities: {} },
    { cnames: 1, domains: 2, entities: 3, trackers: 4 }
]

for (const blockList of invalidBlockLists) {
    await assert.rejects(() =>
        generateTdsRuleset(
            blockList, supportedSurrogateScripts, '', () => { }
        )
    )
fork icon0
star icon0
watch icon0

+ 13 other calls in file

376
377
378
379
380
381
382
383
384
385
        done();
    });
});

it('should not deactivate a plugin if active plugins are set in configuration', (done) => {
    assert.rejects(plugins.toggleActive(activePlugins[0]), Error).then(() => {
        plugins.isActive(activePlugins[0], (err, isActive) => {
            assert.ifError(err);
            assert(isActive);
            done();
fork icon0
star icon0
watch icon1