Skip to content

Commit

Permalink
chore: add runtime tests (#19)
Browse files Browse the repository at this point in the history
Adds tests for new runtimes: [Bun](https://bun.sh/), [Deno](https://deno.land/) and the [Edge runtime](https://edge-runtime.vercel.sh/). I wanted to add tests for node16 ESM modes as well as Cloudflare Workers and react-native maybe. But I'm out of time and it's best to get in what we have and follow up with new PRs.

The Deno test is expected [to keep failing](https://github.com/sanity-io/client/runs/7849905824?check_suite_focus=true#step:5:201) until [`get-it` gets a similar](https://github.com/sanity-io/get-it/blob/7873e3fb4f38c21fe91f727f65acca250bc6c5f0/package.json#L9-L23) fix as #24
  • Loading branch information
stipsan committed Aug 24, 2022
1 parent 3b63119 commit 988208d
Show file tree
Hide file tree
Showing 14 changed files with 15,240 additions and 31 deletions.
4 changes: 1 addition & 3 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
],
"env": {
"test": {
"plugins": [
"istanbul"
]
"plugins": ["istanbul"]
}
}
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ lib
umd
coverage
.nyc_output
runtimes/deno
5 changes: 1 addition & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"extends": [
"sanity",
"prettier"
],
"extends": ["sanity", "prettier"],
"env": {
"node": true,
"browser": true
Expand Down
106 changes: 106 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: CI

on: push

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x]
name: node ${{ matrix.node-version }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- run: npm test

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: 'npm'
- run: npm ci
- run: npm run prepublishOnly
- uses: actions/upload-artifact@v3
with:
name: build-output
path: |
lib/
umd/
prod-deps:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- id: prod-deps
uses: actions/cache@v3
with:
path: ./node_modules
key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- if: steps.prod-deps.outputs.cache-hit != 'true'
uses: actions/setup-node@v3
with:
node-version: lts/*
- if: steps.prod-deps.outputs.cache-hit != 'true'
run: npm install --omit=dev --ignore-scripts

edge-runtime:
runs-on: ubuntu-latest
needs: [build, prod-deps]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: 'npm'
cache-dependency-path: runtimes/edge/package-lock.json
- uses: actions/cache@v3
with:
path: ./node_modules
key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- uses: actions/download-artifact@v3
with:
name: build-output
- run: npm ci
working-directory: runtimes/edge
- run: npm test
working-directory: runtimes/edge

## Temporarily disabled until we can get all downstream dependencies working with deno
# deno-runtime:
# runs-on: ubuntu-latest
# needs: [build, prod-deps]
# steps:
# - uses: actions/checkout@v3
# - uses: actions/cache@v3
# with:
# path: ./node_modules
# key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
# - uses: denoland/setup-deno@v1
# - name: deno test
# run: |
# deno fmt --check
# deno lint
# deno check *.ts
# deno test --allow-net
# working-directory: runtimes/deno

bun-runtime:
runs-on: ubuntu-latest
needs: [build, prod-deps]
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ./node_modules
key: prod-deps-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- uses: antongolub/action-setup-bun@v1
- run: bun wiptest
working-directory: runtimes/bun
20 changes: 0 additions & 20 deletions .github/workflows/test.yml

This file was deleted.

4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"deno.enablePaths": ["runtimes/deno"]
}
Binary file added bun.lockb
Binary file not shown.
24 changes: 24 additions & 0 deletions runtimes/bun/client.fetch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import createClient from '../..'
import {expect, test} from 'bun:test'

const projectId = '81pocpw8'
const dataset = 'production'
const apiVersion = 'v2021-03-25'
const query = /* groq */ `count(*[studioVersion == 3])`

// Ensure the runtime is supposed to be able to run this query correctly
test('native fetch', async () => {
const res = await fetch(
`https://${projectId}.apicdn.sanity.io/${apiVersion}/data/query/${dataset}?query=${encodeURIComponent(
query
)}`
)
const {result: data} = await res.json()
expect(Number.isInteger(data)).toBe(true)
})

test('@sanity/client', async () => {
const client = createClient({projectId, dataset, apiVersion, useCdn: true})
const data = await client.fetch(query)
expect(Number.isInteger(data)).toBe(true)
})
31 changes: 31 additions & 0 deletions runtimes/deno/client_fetch_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { assert } from "https://deno.land/std@0.152.0/testing/asserts.ts";
import { createClient } from "./deps.ts";

const projectId = "81pocpw8";
const dataset = "production";
const apiVersion = "v2021-03-25";
const query = /* groq */ `count(*[studioVersion == 3])`;

// Ensure the runtime is supposed to be able to run this query correctly
Deno.test("native fetch", async () => {
const res = await fetch(
`https://${projectId}.apicdn.sanity.io/${apiVersion}/data/query/${dataset}?query=${
encodeURIComponent(
query,
)
}`,
);
const { result: data } = await res.json();
assert(Number.isInteger(data));
});

Deno.test("@sanity/client fetch", async () => {
const client = createClient({
projectId,
dataset,
apiVersion,
useCdn: true,
});
const data = await client.fetch(query);
assert(Number.isInteger(data));
});
4 changes: 4 additions & 0 deletions runtimes/deno/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @TODO replace with a local path once we start exporting ESM
// import createClient from '../../dist/sanityClient.browser.mjs'
import createClient from "https://esm.sh/@sanity/client";
export { createClient };
25 changes: 25 additions & 0 deletions runtimes/edge/__tests__/client.fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {expect, test} = require('@jest/globals')
const createClient = require('@sanity/client')

const projectId = '81pocpw8'
const dataset = 'production'
const apiVersion = 'v2021-03-25'
const query = /* groq */ `count(*[studioVersion == 3])`

// Ensure the runtime is supposed to be able to run this query correctly
test('native fetch', async () => {
expect.assertions(1)
const res = await fetch(
`https://${projectId}.apicdn.sanity.io/${apiVersion}/data/query/${dataset}?query=${encodeURIComponent(
query
)}`
)
const {result: data} = await res.json()
expect(data).toEqual(expect.any(Number))
})
test('@sanity/client', async () => {
expect.assertions(1)
const client = createClient({projectId, dataset, apiVersion, useCdn: true})
const data = await client.fetch(query)
expect(data).toEqual(expect.any(Number))
})

0 comments on commit 988208d

Please sign in to comment.