Skip to content

Commit

Permalink
chore(cli): configure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed Feb 22, 2021
1 parent e7279fb commit a2e3029
Show file tree
Hide file tree
Showing 28 changed files with 653 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ coverage.data
coverage/
.docusaurus
.idea
packages/botonic-cli/tests/dummy-project/package-lock.json
8 changes: 6 additions & 2 deletions packages/botonic-cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
module.exports = {
roots: ['<rootDir>/src'],
preset: 'ts-jest',
testEnvironment: 'node',
testTimeout: 100000,
roots: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/tests/**/*.+(ts|tsx|js)',
'**/+(*.)+(spec|test).+(ts|tsx|js)',
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!/node_modules/'],
modulePathIgnorePatterns: ['tests/dummy-project/*'],
testPathIgnorePatterns: [
'lib',
'.*.d.ts',
Expand Down
11 changes: 0 additions & 11 deletions packages/botonic-cli/src/botonic-examples.test.ts

This file was deleted.

17 changes: 17 additions & 0 deletions packages/botonic-cli/tests/botonic-api-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { join } from 'path'
import { chdir, cwd } from 'process'

import { BotonicAPIService } from '../src/botonic-api-service'

const workingDirectory = cwd()
const pathToBotonicProject = join(workingDirectory, 'tests', 'dummy-project')
const botonicApiService = new BotonicAPIService()

describe('TEST: BotonicApiService', () => {
it('Builds correctly a project', async () => {
chdir(pathToBotonicProject)
const buildOut = await botonicApiService.build()
expect(buildOut).toBe(true)
chdir(workingDirectory)
})
})
5 changes: 5 additions & 0 deletions packages/botonic-cli/tests/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('TEST: Deploy command', () => {
it('', () => {
expect(true).toBe(true)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const Analytics = require('analytics-node')
const fs = require('fs')
const os = require('os')
const path = require('path')
const { execSync } = require('child_process')

function readJSON(path) {
return JSON.parse(fs.readFileSync(path, 'utf8'))
}

function writeJSON(path, object) {
fs.writeFileSync(path, JSON.stringify(object))
}

function isWindows() {
return os.platform() === 'win32'
}

function execCommand(command) {
try {
return String(execSync(command)).trim()
} catch (e) {
return String(e)
}
}

function getBotonicDependencies() {
try {
const packageJSON = readJSON('package.json')
const botonicDependencies = Object.entries(
packageJSON.dependencies
).filter(([k, _]) => k.includes('@botonic'))
return botonicDependencies
} catch (e) {
return String(e)
}
}

const botonicHomePath = path.join(os.homedir(), '.botonic')
const botonicCredentialsPath = path.join(botonicHomePath, 'credentials.json')
const ANALYTICS_KEY = 'YD0jpJHNGW12uhLNbgB4wbdTRQ4Cy1Zu'

let credentials
const analytics = new Analytics(ANALYTICS_KEY, { flushAt: 1 })

function isAnalyticsEnabled() {
return process.env.BOTONIC_DISABLE_ANALYTICS !== '1'
}

function getSystemInformation() {
return {
platform: os.platform(),
arch: os.arch(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
timestamp: new Date().toISOString(),
is_tty: process.stdout.isTTY,
framework_path: __dirname,
system_path: isWindows()
? execCommand('echo %PATH%')
: execCommand('echo $PATH'),
node_version: execCommand('node --version'),
botonic_cli_version: execCommand('botonic --version'),
botonic_dependencies: getBotonicDependencies(),
}
}

function initializeCredentials() {
if (!fs.existsSync(botonicHomePath)) fs.mkdirSync(botonicHomePath)
const anonymous_id = Math.round(Math.random() * 100000000)
writeJSON(botonicCredentialsPath, { analytics: { anonymous_id } })
}

function readCredentials() {
if (!fs.existsSync(botonicCredentialsPath)) {
initializeCredentials()
}
try {
credentials = readJSON(botonicCredentialsPath)
} catch (e) {
if (fs.existsSync(botonicCredentialsPath)) {
console.warn('Credentials could not be loaded', e)
}
}
}

function track(event, properties = {}) {
if (
isAnalyticsEnabled() &&
analytics &&
credentials &&
credentials.analytics
) {
properties = {
...getSystemInformation(),
...properties,
}
analytics.track({
event: event,
anonymousId: credentials.analytics.anonymous_id,
properties: properties,
})
}
}

const MODE = process.argv[2]
try {
readCredentials()
if (MODE === 'serve') track('Served Botonic CLI')
if (MODE === 'train') track('Trained with Botonic train')
} catch (e) {
console.warn('Error tracking event', e)
}
30 changes: 30 additions & 0 deletions packages/botonic-cli/tests/dummy-project/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This babel configuration is used along with Jest for execute tests,
* do not modify to avoid conflicts with webpack.config.js.
*/

module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
[
'@babel/react',
{
targets: {
node: 'current',
},
},
],
],
plugins: [
require('@babel/plugin-proposal-object-rest-spread'),
require('@babel/plugin-proposal-class-properties'),
require('@babel/plugin-transform-runtime'),
],
}
60 changes: 60 additions & 0 deletions packages/botonic-cli/tests/dummy-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"name": "blank",
"version": "1.0.0",
"scripts": {
"build": "webpack --env target=all --mode=production",
"prestart": "node ./.botonic-telemetry/analytics.js serve",
"start": "webpack serve --env target=dev --mode=development",
"test": "jest",
"pretrain": "node ./.botonic-telemetry/analytics.js train",
"train": "node_modules/.bin/ts-node src/nlu/train-model.ts"
},
"jest": {
"rootDir": "tests",
"transformIgnorePatterns": [
"/node_modules/(?!@botonic).+\\.(js|jsx|ts|tsx|mjs)$"
],
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(scss|css|less)$": "<rootDir>/__mocks__/styleMock.js"
}
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@botonic/react": "~0.18.0"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@babel/preset-react": "^7.12.10",
"@hot-loader/react-dom": "^17.0.1",
"analytics-node": "^3.4.0-beta.3",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.2",
"chokidar": "^3.4.3",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^7.0.0",
"css-loader": "^5.0.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.0.0-alpha.17",
"imagemin-gifsicle": "^6.0.0",
"imagemin-jpegtran": "^6.0.0",
"imagemin-optipng": "^7.0.0",
"imagemin-svgo": "^7.0.0",
"imagemin-webpack": "^5.0.0",
"jest": "^26.6.3",
"node-sass": "^5.0.0",
"null-loader": "^4.0.1",
"react-hot-loader": "^4.13.0",
"sass": "^1.30.0",
"sass-loader": "^10.1.0",
"style-loader": "^2.0.0",
"terser": "^5.5.1",
"terser-webpack-plugin": "^5.0.3",
"webpack": "^5.10.3",
"webpack-cli": "^4.2.0",
"webpack-dev-server": "^4.0.0-beta.0"
}
}
Empty file.
Empty file.
1 change: 1 addition & 0 deletions packages/botonic-cli/tests/dummy-project/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const config = { defaultDelay: 0, defaultTyping: 0 }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const locales = {}
1 change: 1 addition & 0 deletions packages/botonic-cli/tests/dummy-project/src/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const plugins = []
1 change: 1 addition & 0 deletions packages/botonic-cli/tests/dummy-project/src/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const routes = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const webchat = {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const webviews = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test-file-stub'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
21 changes: 21 additions & 0 deletions packages/botonic-cli/tests/dummy-project/tests/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
BotonicInputTester,
BotonicOutputTester,
NodeApp,
} from '@botonic/react'

import { config } from '../src/'
import { locales } from '../src/locales'
import { plugins } from '../src/plugins'
import { routes } from '../src/routes'

const app = new NodeApp({ routes, locales, plugins, ...config })

const i = new BotonicInputTester(app)
const o = new BotonicOutputTester(app)

test('TEST: (404) NOT FOUND', async () => {
await expect(i.text('whatever')).resolves.toBe(
o.text("I don't understand you")
)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DevApp } from '@botonic/react'

import { config } from '../src'
import { locales } from '../src/locales'
import { plugins } from '../src/plugins'
import { routes } from '../src/routes'
import { webchat } from '../src/webchat'

export const app = new DevApp({
routes,
locales,
plugins,
...webchat,
...config,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NodeApp } from '@botonic/react'

import { config } from '../src'
import { locales } from '../src/locales'
import { plugins } from '../src/plugins'
import { routes } from '../src/routes'

export const app = new NodeApp({ routes, locales, plugins, ...config })
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { WebchatApp } from '@botonic/react'

import { webchat } from '../src/webchat'

export const app = new WebchatApp(webchat)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { WebviewApp } from '@botonic/react'

import { locales } from '../src/locales'
import { webviews } from '../src/webviews'

export const app = new WebviewApp({ webviews, locales })
Loading

0 comments on commit a2e3029

Please sign in to comment.