Skip to content

Testing workflows locally with vitest #888

@BarakChamo

Description

@BarakChamo

Hello,

I'm working on a project where workflows are set up in a package inside a monorepo, separated from a Next.js application where they are used.

I'm trying to set up testing but could not find any reference on how to best test workflows, so trying to get things working in vitest based on the Vite integration guide.

I have a very simple workflow taken from the repo:

async function add(num: number, num2: number): Promise<number> {
	'use step'
	return num + num2
}

export async function addition(num: number, num2: number): Promise<number> {
	'use workflow'
	const result = await add(num, num2)
	console.log({ result })
	return result
}

And a very simple test, also taken from the repo (with adjustments to test workflows directly):

import { describe, expect, it } from 'vitest'
import { start } from 'workflow/api'
import { addition } from '.'

describe('addition workflow', () => {
	it('adds two numbers correctly', async () => {
		// Start the workflow run with inputs 1 and 2
		const run = await start(addition, [1, 2])

		// Await workflow completion and get the result
		const result = await run.returnValue

		expect(result).toBe(3)
		expect(run.runId).toMatch(/^wrun_.+/)
	})
})

I'm able to get the vitest setup to handle the directives with a basic configuration:

import { nitro } from 'nitro/vite'
import { defineProject } from 'vitest/config'
import { workflow } from 'workflow/vite'

export default defineProject({
        // not sure if nitro is even needed here
	plugins: [nitro(), workflow()],
	test: {
		environment: 'node',
		// setupFiles: ['./vitest.setup.ts'],
		include: ['**/*.test.ts'],
	},
})

However, when I run the tests, I get the following error:

[local world] Queue operation failed: Error: Unable to resolve base URL for workflow queue.
    at resolveBaseUrl

This makes sense, as this is running in vitest and not a fully running application, and I understand the local world requires the port and base url to handle queuing:
https://useworkflow.dev/worlds/local#configuration

My question is:

  1. Is there a reference on how to test workflows with vitest or any other testing setup?

  2. With the setup above, what am I missing to get this to work?
    I've looked through the vite setup but still not clear on how to correctly configure things.

https://useworkflow.dev/docs/getting-started/vite#configure-vite

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions