firebase-js-sdk
firebase-js-sdk copied to clipboard
@firebase/firestore: Firestore (9.23.0): FIRESTORE (9.23.0) INTERNAL ASSERTION FAILED: Unexpected state
Operating System
Mac OS X 13.6
Browser Version
Headless
Firebase SDK Version
9.23
Firebase SDK Product:
Firestore
Describe your project's tooling
I am using Angular wth NX workspace that is running JEST to execute test. I am using @angular/fire 7.6.1
Describe the problem
I am unable to execute a test that is failing on
[2023-10-20T09:08:32.007Z] @firebase/firestore: Firestore (9.23.0): FIRESTORE (9.23.0) INTERNAL ASSERTION FAILED: Unexpected state
at Logger.defaultLogHandler [as _logHandler] (node_modules/@firebase/logger/src/logger.ts:115:57)
at Logger.Object.<anonymous>.Logger.error (node_modules/@firebase/logger/src/logger.ts:210:5)
at logError (node_modules/@firebase/firestore/src/util/log.ts:59:15)
at fail (node_modules/@firebase/firestore/src/util/assert.ts:35:3)
at hardAssert (node_modules/@firebase/firestore/src/util/assert.ts:54:5)
at fromBytes (node_modules/@firebase/firestore/src/remote/serializer.ts:264:5)
at fromWatchChange (node_modules/@firebase/firestore/src/remote/serializer.ts:502:25)
at PersistentListenStream.onMessage (node_modules/@firebase/firestore/src/r
I saw many similar issues over here but unfortunatelly I have no idea how to fix it in this setup. One more thing that I need to say, when I run a test to create a document it works, but when I try to retrieve it this is where I get this error.
Steps and code to reproduce issue
I have created the repo to test. Please follow quict instruction in the REDME. Thank you
https://github.com/fkolar/firebase-test
The same works with karma:
https://github.com/fkolar/firebase-karma
When I added a console.log to the file that does the assert
function fromBytes(serializer, value) {
if (serializer.useProto3Json) {
hardAssert(value === undefined || typeof value === 'string');
return ByteString.fromBase64String(value ? value : '');
}
else {
console.log('@@@@@@@@@@@@@@@@@@@@@: ', value)
console.log('@@@@@@@@@@@@@@@@@@@@@: ', value === undefined || value instanceof Uint8Array)
hardAssert(value === undefined || value instanceof Uint8Array);
return ByteString.fromUint8Array(value ? value : new Uint8Array());
}
}
The output is
@@@@@@@@@@@@@@@@@@@@@: <Buffer >
@@@@@@@@@@@@@@@@@@@@@: false
Thanks for reporting @fkolar and for providing a reproduction repository.
I wonder if you guys have any workaround in meanwhile. I dont really want to more all to Karma yet.
I can reproduce the issue with the provided repo, but after spending time trying to resolve it, I don't have a workaround for you. There is another thread with several suggestions that may be helpful for you: https://github.com/firebase/firebase-js-sdk/issues/6931.
@hsubox76, do you have any thoughts on this one?
@fkolar To clarify, I think what is happening is that your build is choosing the wrong entry point/bundle for the Firebase SDK when building for tests. E.g. it's using the node entry point instead of the browser entry point. I'm not familiar enough with your tool chain to advise you how to change this.
Thanks @fkolar for the repros, by the way, they were very helpful.
The one bit of info I can offer is that the Jest test grabs the Node CJS bundle (@firebase/firestore/dist/index.node.cjs.js
) while the Karma test grabs the browser ESM bundle (@firebase/firestore/dist/index.esm2017.js
). I think the bundle you actually want in Jest is the browser CJS bundle (@firebase/firestore/dist/index.cjs.js
).
As for how to get it, I managed to do so with a custom resolver:
Add resolver: "<rootDir>/jest.resolver.js"
to jest.config.ts
Create jest.resolver.js
in the root dir with the contents:
// jest.resolver.js
module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
conditions: ['browser', 'require']
});
};
This doesn't seem very robust, especially as it will affect all modules (including non-firebase ones) but basically you have to tinker with condition order until you make sure you're resolving to this condition for firestore: https://github.com/firebase/firebase-js-sdk/blob/f002ef36a6b427fd526696f9cd6077a217ccc6ef/packages/firestore/package.json#L66
As an aside, when I did that, I got past the error above but ran into a timeout error which may be something else, or just because I haven't got the emulator set up.
You may also be able to use the packageFilter
option to make a resolution change only apply to @firebase/firestore
. There's an example of how to use it here, but I wouldn't make the exact changes they make there - it won't work, it will just end up resolving to index.node.cjs.js again, as that's what "main" is. I'd probably modify exports.node.require
(I think that's the field Jest will always grab) to point to index.cjs.js
Hey @fkolar. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
This is great info, in meantime I am using Karma and all works well, but I will try to this resolver thing..
I was getting a similar error when using IndexDB persistence with Firestore. Just ended up disabling persistence and things are working for me.