先ずは、node.js

かなり古めが入っていたのでバージョンアップ
ダウンロード
https://nodejs.org/en

インストーラー
node-v20.12.1-x64.msi


C:\nodej>node -v
v20.12.1
次に nodej でサンプル web server を立ち上げてみる。
>node
>.help
>.edtiror
////// code
const http = require('node:http');
const myHostname = '127.0.0.1';
const port = 3001;
const myServer = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World oooooooooo!\n');
});
myServer.listen(port, myHostname , () => {
console.log(`My Server running at http://${hostname}:${port}/`);
});
//////

Ctrl + D

次に、雛形プロジェクト作成ツール Vue CLI を入れる。
> npm install -g @vue/cli

・・・・
・・・・

プロジェクトを生成
>vue create mycli

([Vue 2] babel, eslint ) 選択

C:\nodej\mycli\src
→ 生成したコードを適度に修正してみる。
C:\nodej\mycli\src\main.js
※./App.vue のimport別名を変更
--------------------------------------
import Vue from 'vue'
import AppMy from './App.vue' // ※
Vue.config.productionTip = false
new Vue({
render: h => h(AppMy),
}).$mount('#app')
--------------------------------------
C:\nodej\mycli\src\App.vue
・コンポーネントの名称を HelloWorldTomo に変更
・コンポーネント props プロパティ名称を msgTomo: String に変更
-------------------------------------------------------------------------------------
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<HelloWorldTomo msgTomo="Welcome to Your Vue.js App"/>
</div>
</template>
<script>
import HelloWorldTomo from './components/MyHelloWorldCompo.vue'
export default {
name: 'App',
components: {
HelloWorldTomo
}
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
C:\nodej\mycli\src\components\MyHelloWorldCompo.vue
・コンポーネント props プロパティ名称を msgTomo: String に変更
-------------------------------------------------------------------------------------
<template>
<div class="hello">
<h1>{{ msgTomo }}</h1>
<p>
For a guide and recipes on how to configure / customize this project,<br>
check out the
<a href="https://cli.vuejs.org" target="_blank" rel="noopener">vue-cli documentation</a>.
</p>
<h3>Installed CLI Plugins</h3>
<ul>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-babel" target="_blank" rel="noopener">babel</a></li>
<li><a href="https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-eslint" target="_blank" rel="noopener">eslint</a></li>
</ul>
</ul>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
msgTomo: String
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
</style>
-------------------------------------------------------------------------------------
### Compiles and hot-reloads for development
> npm run serve


次に、Jest と vue-test-utils
をインストール ( JavaのJUnit相当 )
(参考) https://zenn.dev/hrtk/scraps/3107800740739e
> vue add unit-jest

以下、自動生成テストコード
C:\nodej\mycli\tests\unit
example.spec.js
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message'
const wrapper = shallowMount(HelloWorld, {
propsData: { msg }
})
expect(wrapper.text()).toMatch(msg)
})})
適度に編集したソースに合わせて、テストコードを編集する
import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/MyHelloWorldCompo.vue'
describe('HelloWorldTEST.vue', () => {
it('renders props.msg when passed.....', () => {
const msgTomo = 'new message meeee'
const wrapper = shallowMount(HelloWorld, {
propsData: { msgTomo }
})
expect(wrapper.text()).toMatch(msgTomo) /// toMatch で期待値と実測値を比較検証
})
})
package.json
{
"name": "mycli",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"test:unit": "vue-cli-service test:unit",
"lint": "vue-cli-service lint"
},
・・・・・
・・・・・
テスト実施
>npm run test:unit

最近のコメント