Skip to content

Commit

Permalink
test(cli): provide full cli tests for both v2 and v3
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Oct 6, 2022
1 parent 27d5085 commit 5412ca8
Show file tree
Hide file tree
Showing 59 changed files with 2,494 additions and 355 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,3 @@ jobs:
- name: Test
id: test
run: yarn test

# Required for import lint rules to work - packages pointing to ex
# `lib/index.js`, where `src/index.ts` gets compiled to `lib/index.js`
- name: Build modules
id: buildResolver
run: yarn build
env:
NODE_OPTIONS: --max_old_space_size=8192

- name: Test CLI
working-directory: ${{ env.GITHUB_WORKSPACE }}
run: |
node scripts/testCli.js
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
"start-server-and-test": "^1.14.0",
"tmp": "^0.2.1",
"typescript": "^4.8.3",
"undici": "^5.8.2",
"yargs": "^17.3.0"
},
"optionalDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions packages/@sanity/cli/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

const {createJestConfig} = require('../../../test/config.cjs')

module.exports = createJestConfig({
displayName: require('./package.json').name,
testEnvironment: 'node',
rootDir: __dirname,
globalSetup: '<rootDir>/test/shared/globalSetup.ts',
globalTeardown: '<rootDir>/test/shared/globalTeardown.ts',
slowTestThreshold: 60000,
testTimeout: 30000,
})
5 changes: 4 additions & 1 deletion packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"postbuild": "run-s check:package",
"prebuild": "run-s clean",
"watch": "pkg-utils watch --tsconfig tsconfig.lib.json",
"ts": "node -r esbuild-register"
"ts": "node -r esbuild-register",
"test": "jest"
},
"dependencies": {
"@babel/traverse": "^7.19.0",
Expand All @@ -60,6 +61,7 @@
"@sanity/types": "3.0.0-dev-preview.20",
"@sanity/util": "3.0.0-dev-preview.20",
"@types/configstore": "^5.0.1",
"@types/cpx": "^1.5.2",
"@types/babel__traverse": "^7.18.1",
"@types/decompress": "^4.2.4",
"@types/eventsource": "^1.1.8",
Expand All @@ -73,6 +75,7 @@
"chalk": "^4.1.2",
"clean-stack": "^3.0.0",
"configstore": "^5.0.1",
"cpx": "^1.5.0",
"debug": "^3.2.7",
"decompress": "^4.2.0",
"deep-sort-object": "^1.0.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/@sanity/cli/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"max-nested-callbacks": "off"
}
}
Binary file not shown.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'
import styles from './MyLogo.module.css'

// Test that we can import CSS modules and that they get bundled
export function MyLogo() {
return <div className={styles.logo}>Hello</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.logo {
background: green;
}
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v2/document.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"_id": "drafts.magnus-carlsen",
"_type": "person",
"name": "Magnus Carlsen"
}
26 changes: 26 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "v2-studio",
"private": true,
"version": "0.0.1",
"description": "A Sanity v2 studio for CI testing.",
"author": "Sanity.io <hello@sanity.io>",
"license": "MIT",
"scripts": {
"build": "sanity build",
"start": "sanity start"
},
"keywords": [
"fixture"
],
"dependencies": {
"@sanity/base": "^2.33.3",
"@sanity/cli": "^2.33.3",
"@sanity/core": "^2.33.3",
"@sanity/default-layout": "^2.33.3",
"@sanity/default-login": "^2.33.3",
"@sanity/desk-tool": "^2.33.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"styled-components": "^5.2.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@
"name": "v2 studio"
},
"api": {
"projectId": "ppsg7ml5",
"dataset": "test"
"projectId": "aeysrmym",
"dataset": "production"
},
"plugins": [
"@sanity/base",
"@sanity/default-layout",
"@sanity/default-login",
"@sanity/desk-tool"
],
"server": {
"port": 3334
},
"parts": [
{
"name": "part:@sanity/base/schema",
"path": "schema"
},
{
"implements": "part:@sanity/base/brand-logo",
"path": "./components/MyLogo.jsx"
}
]
}
38 changes: 38 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v2/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable import/no-unresolved */
import schemaTypes from 'all:part:@sanity/base/schema-type'
import createSchema from 'part:@sanity/base/schema-creator'

export default createSchema({
name: 'v2-schema',
types: schemaTypes.concat([
{
name: 'person',
title: 'Person',
type: 'document',
fields: [
{
name: 'name',
title: 'Name',
type: 'string',
},
{
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'name',
maxLength: 100,
},
},
{
name: 'image',
title: 'Image',
type: 'image',
options: {
hotspot: true,
},
},
],
},
]),
})
11 changes: 11 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v2/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable import/no-unresolved, no-console */
// Tests that `sanity exec` in v2 can import parts, and use a preconfigured client
import client from 'part:@sanity/base/client'

client
.withConfig({apiVersion: '2022-09-09'})
.users.getById('me')
.then(
(user) => console.log(JSON.stringify(user, null, 2)),
(err) => console.error(err)
)
5 changes: 5 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"import/no-extraneous-dependencies": "off"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'
import styles from './MyLogo.module.css'

// Test that we can import CSS modules and that they get bundled
export function MyLogo() {
return <div className={styles.logo}>Hello</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.logo {
background: green;
}
5 changes: 5 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/document.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"_id": "drafts.magnus-carlsen",
"_type": "person",
"name": "Magnus Carlsen"
}
21 changes: 21 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "v3-studio",
"private": true,
"version": "0.0.1",
"description": "A Sanity v3 studio for CI testing.",
"author": "Sanity.io <hello@sanity.io>",
"license": "MIT",
"scripts": {
"build": "sanity build",
"start": "sanity start"
},
"keywords": [
"fixture"
],
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sanity": "3.0.0-dev-preview.20",
"styled-components": "^5.2.0"
}
}
15 changes: 15 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/sanity.cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {createCliConfig} from 'sanity/cli'

export default createCliConfig({
api: {
projectId: 'aeysrmym',
dataset: 'production',
},

graphql: [
{
playground: false,
workspace: 'default', // @todo remove
},
],
})
11 changes: 11 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/sanity.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createConfig} from 'sanity'
import {MyLogo} from './components/MyLogo'
import {schema} from './schema'

export default createConfig({
name: 'default', // @todo remove
projectId: 'aeysrmym',
dataset: 'production',
logo: MyLogo,
schema,
})
34 changes: 34 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export const schema = {
name: 'default',
types: [
{
name: 'person',
title: 'Person',
type: 'document',
fields: [
{
name: 'name',
title: 'Name',
type: 'string',
},
{
name: 'slug',
title: 'Slug',
type: 'slug',
options: {
source: 'name',
maxLength: 100,
},
},
{
name: 'image',
title: 'Image',
type: 'image',
options: {
hotspot: true,
},
},
],
},
],
}
11 changes: 11 additions & 0 deletions packages/@sanity/cli/test/__fixtures__/v3/script.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable no-console */
// Tests that `sanity exec` in v3 can import the CLI client
import {getCliClient} from 'sanity/cli'

getCliClient()
.withConfig({apiVersion: '2022-09-09'})
.users.getById('me')
.then(
(user) => console.log(JSON.stringify(user, null, 2)),
(err) => console.error(err)
)

0 comments on commit 5412ca8

Please sign in to comment.