mocha, chai, Uncaught AssertionError: expected {} to equal {} + expected - actual [duplicate] - mocha.js

This question already has answers here:
chai test array equality doesn't work as expected
(7 answers)
Closed 6 years ago.
it('GET /customers/ with wrong id', (done) => {
request
.get(`/customers/${wrongId}`)
.end((err, res) => {
expect(res.body).to.equals({});
expect(res).to.have.status(404);
done();
});
});
1) Customers CRUD GET /customers/ with wrong id:
Uncaught AssertionError: expected {} to equal {}
+ expected - actual

You want to use deep if you're trying to compare objects:
expect(res.body).to.deep.equal({});
Or use the eql method:
expect(res.body).to.eql({});

Related

Failure: Inconsistent type in JSPB repeated field array. Got undefined expected object

Inconsistent type in JSPB repeated field array. Got undefined expected object +AssertionError: Failure: Inconsistent type in JSPB repeated field array. Got undefined expected object
I am using fabric, and I think this is a problem with the chain code, but can't point the problem.
here is the stacktrace:
Fri, 29 Apr 2022 13:12:24 GMT + Failure: Inconsistent type in JSPB repeated field array. Got undefined expected object +AssertionError: Failure: Inconsistent type in JSPB repeated field array. Got undefined expected object
at new goog.asserts.AssertionError (/Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:87:1065)
at Object.goog.asserts.fail (/Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:90:89)
at /Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:502:168
at Array.forEach (<anonymous>)
at Object.goog.array.forEach (/Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:101:188)
at Function.jspb.Message.assertConsistentTypes_ (/Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:502:114)
at Function.jspb.Message.bytesListAsU8 (/Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:501:217)
at proto.protos.ChaincodeInput.getArgsList_asU8 (/Users/user/hl-f/backend/node_modules/#hyperledger/fabric-gateway/dist/protos/peer/chaincode_pb.js:557:59)
at proto.protos.ChaincodeInput.serializeBinaryToWriter (/Users/user/hl-f/backend/node_modules/#hyperledger/fabric-gateway/dist/protos/peer/chaincode_pb.js:508:15)
at jspb.BinaryWriter.writeMessage (/Users/user/hl-f/backend/node_modules/google-protobuf/google-protobuf.js:451:479)+
the code that do this is a simple AddAsset query that i modified,
#Transaction()
public async AddAsset(ctx: Context, assetid : string,email: string,phone :string,address :string): Promise<void> {
const exists = await this.AssetExists(ctx, assetid);
if (exists) {
throw new Error(`The asset ${assetid} already exists`);
}
const asset = {
AssetID: assetid,
Email: email,
Phone: phone,
Address: address,
};
await ctx.stub.putState(assetid, Buffer.from(stringify(sortKeysRecursive(asset))));
}
Always double check, triple check your app/backend, when encountering this problem.
you might have a similar problem to this:
json = {
Assetid : "123",
...
}
contract.submitTransaction("AddAsset", json.AssetID, ...);
explanation:
json.Assetid = 123
while
json.AssetID = undefined
I feel stupid, on how much time i wasted on this bug, thinking it was the chaincode,
And remember double, triple check.
And happy coding.

Cypress - first test randomly fails with "Invalid or unexpected token"

Recently switched to using Cypress parallel for our Angular project in our pipeline. We run on a Codebuild on AWS and run 5 threads of the Cypress runner. About a quarter of the time, the first test on one of the threads fails with this error:
An uncaught error was detected outside of a test
Invalid or unexpected token
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test. We dynamically generated a new test to display this failure.
Tried many things to try to fix this, including setting modifyObtrusiveCode to false, chromeWebSecurity to false, upgrading Cypress. We are already catching uncaught exceptions so that doesn't seem like it should be the issue. I turned on some extra logs for this and here is the output
[3] 2020-03-06T19:57:20.369Z cypress:server:project onMocha start
[3] 2020-03-06T19:57:20.369Z cypress:server:reporter got mocha event 'start' with args: [ { start: '2020-03-06T19:57:20.366Z' } ]
[3] 2020-03-06T19:57:20.374Z cypress:server:project onMocha suite
[3] 2020-03-06T19:57:20.374Z cypress:server:reporter got mocha event 'suite' with args: [ { id: 'r1', title: '', root: true, type: 'suite', file: 'cypress/integration/ci-tests/content-acquisition/channels/channel-manual-upload-run-acquired-items-tab.spec.ts' } ]
[3]
[3] 2020-03-06T19:57:20.390Z cypress:server:project onMocha test
[3] 2020-03-06T19:57:20.391Z cypress:server:reporter got mocha event 'test' with args: [ { id: 'r2', title: 'An uncaught error was detected outside of a test', body: 'function throwErr() {\n throw err;\n }', type: 'test' } ]
[3] 2020-03-06T19:57:20.555Z cypress:server:reporter got mocha event 'fail' with args: [ { id: 'r2', title: 'An uncaught error was detected outside of a test', err: { message: 'Unexpected end of input\n' + '\n' + 'This error originated from your test code, not from Cypress.\n' + '\n' + 'When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.\n' + '\n' + 'Cypress could not associate this error to any specific test.\n' + '\n' + 'We dynamically generated a new test to display this failure.', name: 'Uncaught SyntaxError', stack: 'Uncaught SyntaxError: Unexpected end of input\n' + '\n' + 'This error originated from your test code, not from Cypress.\n' + '\n' + 'When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.\n' + '\n' + 'Cypress could not associate this error to any specific test.\n' + '\n' + 'We dynamically generated a new test to display this failure.' }, state: 'failed', body: 'function throwErr() {\n throw err;\n }', type: 'test', duration: 179, wallClockStartedAt: '2020-03-06T19:57:20.374Z', timings: { lifecycle: 26, test: [Object] } } ]
I couldn't really make anything of these errors, but maybe someone else can. I'm kind of out of ideas on what to try (I've tried more things today than I've listed but can't recall them all). Any ideas?
as setting modifyObtrusiveCode to false didn't help you as the folks in https://github.com/cypress-io/cypress/issues/6132 .. I can give my debug procedure when I encountered a similar flakey "unexpected .." error with Cypress:
cypress run has a burn= param, able to repeatedly run. Enable .har output recording for those runs with the cypress-har-generator plugin.
When you have two groups of successful and failing example .har files for the same request, open them in a Browser to compare if anything stands out.
I used diff + jq queries on the .har files to compare between the groups content per request path, but already opening a failing .har in the browser inspector network tab showed a 30s processing time for a .js path that was ultimately incomplete, and thus violated js syntax, causing an unexpected end of input error, similar to your "unexpected token".
Interestingly this occured to the same file at the same code line, hinting at a parsing problem in Cypress.
We exchanged that dependency (or specifically - updated it and changed how it was webpacked) and Cypress stopped to hiccup on the ressource, the flakiness disappeared.
My impression is, running parallel threads of Cypress contributes to the problem occuring.

'contains()' is unavailable: call the 'contains()' method on the sequence [duplicate]

This question already has answers here:
Can't use method "contains" in Swift 2
(2 answers)
Closed 6 years ago.
How do I "Call the 'contains()' method on the sequence?"
func deleteSelfieObjectFromList(selfieImgObject: SelfieImage) {
if contains(self.dataArray, selfieImgObject) {
removeObject(&self.dataArray, object: selfieImgObject)
self.collectionView?.reloadData()
}
}
Like this:
let arr = [42]
if arr.contains(42) {
print("found")
}

Jest silently ignores errors

Given the following test.js
var someCriticalFunction = function() {
throw 'up';
};
describe('test 1', () => {
it('is true', () => {
expect(true).toBe(true);
})
});
describe('test 2', () => {
it('is ignored?', () => {
someCriticalFunction();
expect(true).toBe(false);
})
});
Running Jest will output
Using Jest CLI v0.9.2, jasmine2
PASS __tests__/test.js (0.037s)
1 test passed (1 total in 1 test suite, run time 0.795s)
Which pretty much gives you the impression everything is great. So how do I prevent shooting my own foot while writing tests? I just spent an hour writing tests and now I wonder how many of them actually run? Because I will certainly not count all of them and compare with the number in the report.
Is there a way to make Jest fail as loud as possible when an error is thrown as part of the test suite, not the tests itself (like preparation code)? Putting it inside beforeEach doesn't change anything.
This is fixed in 0.9.3 jest-cli https://github.com/facebook/jest/pull/812
Which for some reason is unpublished again (I had it installed minutes ago). So anyway, throwing strings is now caught. You should never throw strings anyway and thanks to my test suite I found one of the last places I actually still threw strings...

mocha hooks, only methods not working

The mocha hooks like before(), after(), beforeEach(), afterEach() are not working. Also the only method too not working. None of the beforeEach is called. I get an error has no method 'only'. Below is the code.
describe('Array', function(){
beforeEach(function(){
console.log('before every test')
})
describe.only('#indexOf()', function(){
beforeEach(function(){
console.log('before every test')
})
it.only('should return -1 unless present', function(){
assert.equal(1,2)
})
it('should return the index when present', function(){
assert.equal(1,2);
})
})
})
beforeEach(function(){
console.log('before every test')
})
Your code works for me. You need to add:
var assert = require('assert');
at top. And, be sure you npm install -g mocha.
Then, I get:
$ mocha -R spec temp.js
Array
#indexOf()
◦ should return -1 unless present: before every test
before every test
before every test
1) should return -1 unless present
◦ should return the index when present: before every test
before every test
before every test
2) should return the index when present
0 passing (16ms)
2 failing
1) Array #indexOf() should return -1 unless present:
AssertionError: 1 == 2
at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:12:16)
at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
2) Array #indexOf() should return the index when present:
AssertionError: 1 == 2
at Context.<anonymous> (/Users/dan/Dropbox/Documents/dev/spock/temp.js:15:16)
at Test.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:211:32)
at Runner.runTest (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:355:10)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:401:12
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:281:14)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:290:7
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:234:23)
at /usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:253:7
at Hook.Runnable.run (/usr/local/share/npm/lib/node_modules/mocha/lib/runnable.js:213:5)
at next (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:246:10)
at Object._onImmediate (/usr/local/share/npm/lib/node_modules/mocha/lib/runner.js:258:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
Note that your it.only is being ignored because the describe.only is encountered first. If you aren't able to replicate this output, please report what version of node and npm you're using.

Resources