Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Commit

Permalink
feat: Add basic TestCafe suite
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidmt committed Feb 24, 2021
1 parent 94ee7bc commit ff88510
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test.app/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!doctype html>
<html lang="en">
<head>
<style>
.test {
width: 500px;
margin: 30px auto;
padding: 30px;
border: 1px solid gray;
}
.description {
font-style: italic;
margin-bottom: 30px;
}
</style>
</head>
<body>
<div class="test">
<div class="description">
<strong>1.</strong> Obtain IP address using <code><strong>GET:</strong> https://api.ipify.org</code>
</div>
<div id="get-result">-</div>
</div>
</body>
</html>
20 changes: 20 additions & 0 deletions test.app/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const qs = require("qs")
const { GET, setup } = require("../../src")

setup({
stringifyQueryParams: source => qs.stringify(source),
})

GET("https://api.ipify.org", {
query: {
format: "json",
},
})
.then(data => {
document.querySelector("#get-result").innerHTML = `OK ${
data.ip.split(".").length
}`
})
.catch(error => {
document.querySelector("#get").innerHTML = error.message
})
47 changes: 47 additions & 0 deletions test.app/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-env node */

const path = require("path")
const HtmlWebPackPlugin = require("html-webpack-plugin")

module.exports = {
entry: "./test.app/src/index.js",

output: {
publicPath: "/",
path: path.resolve(__dirname, "dist"),
filename: "[name].js",
},

devtool: false,

module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
},
},
],
},

plugins: [
new HtmlWebPackPlugin({
template: "test.app/src/index.html",
}),
],

optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
vendor: {
test: /[/\\]node_modules[/\\]/,
name: "vendors",
chunks: "all",
},
},
},
},
}
6 changes: 6 additions & 0 deletions test.scenarios/GET.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Selector as $ } from "testcafe"

fixture(`asd14/fetch-browser`).page(`http://localhost:50123`)

test("GET: 200 with json response", t =>
t.expect($("#get-result").textContent).eql("OK 4"))
32 changes: 32 additions & 0 deletions test.scenarios/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable promise/catch-or-return */

/**
* Local static server serving index.html running the compiled version
* of `@asd14/fetch-browser`
*/
const httpServer = require("http-server").createServer({
root: "test.app/dist",
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
},
})

httpServer.listen(50123)

/**
* Run all tests via local test runner
*/
const createTestCafe = require("testcafe")

createTestCafe("localhost", 1337, 1338).then(async testcafe => {
const runner = testcafe.createRunner()

await runner
.src(["test.scenarios/GET.test.js"])
.browsers(["firefox:/usr/bin/firefox-developer-edition"])
.run()

testcafe.close()
httpServer.close()
})

0 comments on commit ff88510

Please sign in to comment.