Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please, could you demo some Unit Tests with Vitest and Firestore #31

Open
ghost opened this issue Jun 8, 2022 · 2 comments
Open

Please, could you demo some Unit Tests with Vitest and Firestore #31

ghost opened this issue Jun 8, 2022 · 2 comments

Comments

@ghost
Copy link

ghost commented Jun 8, 2022

I ve asked a question here: https://stackoverflow.com/questions/72539977/error-when-using-firestore-and-vitest-i-cannot-add-items-with-firestore-executi

You offer some tests, but I think there is just auth tests concerned. I am looking to use firestore with Vitest and it doesnt work, could you insert an example of a unit test using a firestore CRUD operation ?

Thansk you,

here is my test

import {readFileSync, createWriteStream} from 'fs'
import {initializeTestEnvironment, assertFails, assertSucceeds} from '@firebase/rules-unit-testing'
import {doc, getDoc, setDoc, serverTimestamp, setLogLevel} from 'firebase/firestore'

let db
let app
let testEnv

beforeAll(async () => {
	// const config = {
	// 	apiKey: 'AIzaSyCIFKC20rq6kAglerGHVLQDDqFBrzjmx0M',
	// 	authDomain: 'tinax-app.firebaseapp.com',
	// 	projectId: 'tinax-app'
	// }
	setLogLevel('error')

	testEnv = await initializeTestEnvironment({
		projectId: 'tinax-app',
		firestore: {host: 'localhost', port: 8080}
	})

	// app = initializeApp(config)
	// db = getFirestore(app)
	// if (getApps().length === 0) connectFirestoreEmulator(db, 'localhost', 8080)
})

describe('TESTING TIN--2...', () => {
	it('TEST_1', async () => {
		// [START set_document]
		try {
			await testEnv.withSecurityRulesDisabled(async (context) => {
				await setDoc(doc(context.firestore(), 'users/foobar'), {foo: 'bar'})
			})
			// if (browser) {
			// const docRef = doc(db, 'cities', 'LA')
			// console.log('starting set...')
			// await setDoc(docRef, {
			// 	name: 'Los Angeles',
			// 	state: 'CA',
			// 	country: 'USA'
			// })
			// expect(true).toBeTruthy
			// } else {
			// console.log('cannot exec test in server side')
			// }
		} catch (e) {
			console.log(e)
			// Add a new document in collection "cities"
		}
		// [END set_document]
	}, 10000)
})

and here is the error:

stdout | src/lib/firebase/dao.test.ts > TESTING TIN--2... > TEST_1
[FirebaseError: Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore] {
  code: 'invalid-argument',
  customData: undefined,
  toString: [Function (anonymous)]
}
@jmagrippis
Copy link
Owner

Hello @regnou . Great to see you're getting somewhere using SvelteKit and Firebase, and want to do tests with Vitest too! I'm excited for you.

It's worth noting this repo isn't a "starter"... of course, you're free to clone it, or fork it, or copy & paste from it and use it however you want to help you get started if you wish. If I didn't want that, I wouldn't have made it public or link to it from my videos!

But mostly I'm using it as a demo, and to highlight things I wanna show in my videos. If I make a video about using the Firestore, I'll make sure to have a section to talk about testing, and have done the relevant code here to showcase. But as you may know, it does take me a lot of time to make these videos, so I gotta pick the subjects carefully... and I think there are many things I'd want to cover before we get to using the Firestore!

(the video I'm editing currently is on SvelteKit Endpoints for example)

So, I am noting this as a video idea, but just setting expectations that I'm unlikely to act on it...


Also: Your issue seems to be around Firebase Unit Testing, it's not Svelte or SvelteKit or Vitest specific! You may have more luck investigating that, maybe that @firebase/rules-unit-testing library?

Alternatively, you could get around all of this if you did E2E testing with Playwright or Cypress instead...

@ghost
Copy link
Author

ghost commented Jun 8, 2022

I will try to have it working and I have created a demo for that: https://github.com/regnou/vitest_firebase_demo

Thanks you very much @jmagrippis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant