We're trying to query Firestore via a k6 script on a collection.
In order to integrate the Firestore library we followed this guide on k6 website:
https://k6.io/docs/using-k6/modules/#setting-up-the-bundler
When we run our script we get this weird error, that we can't find on Firestore documentation:
@firebase/firestore: Firestore (8.1.2): AsyncQueue Initialization of query 'Query(target=Target(my_collection_here, orderBy: [timestamp (asc), name (asc)]); limitType=F)' failed: TypeError: Value is not an object: undefined
The script is this:
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import firebase from 'firebase/app';
import 'firebase/firestore';
import { sleep } from 'k6';
import behavior from './tests/behaviors/index.js';
import scenariosAndStages from './tests/options/index.js';
const firebaseConfig = {
apiKey: ***,
authDomain: ***,
projectId: ***,
storageBucket: ***,
messagingSenderId: ***,
appId: ***,
};
firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const conf = {
multiply: __ENV.MULTIPLY || 1,
};
export const options = {
ext: {
loadimpact: {
name: __ENV.NAME,
projectID: __ENV.PROJECT_ID,
distribution: {
'amazon:us:ashburn': { loadZone: 'amazon:us:ashburn', percent: 100 },
},
},
},
discardResponseBodies: true,
scenarios: __ENV.SCENARIOS ? { [__ENV.SCENARIOS]: scenariosAndStages.scenarios[__ENV.SCENARIOS] } : undefined,
stages: scenariosAndStages.stages[__ENV.STAGES],
};
export default () => {
db
.collection('my_collection_here')
.orderBy('timestamp')
.get()
.then(console.log);
sleep(10);
behavior.myBehavior('application load test one call', conf.multiply);
};
What are we doing wrong?
After some light research I've come to realize that the Firestore JS SDK requires Node.js in order to function, making it incompatible with k6 according to k6's documentation.
I would recommend the Firebase emulator for unit testing. If one would prefer to mock certain functions for testing purposes, I would recommend using Jest instead, as shown here